惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
U
Unit 42
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Martin Fowler
Martin Fowler
L
LangChain Blog
博客园_首页
博客园 - 【当耐特】
Vercel News
Vercel News
I
InfoQ
GbyAI
GbyAI
爱范儿
爱范儿
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
B
Blog RSS Feed
A
About on SuperTechFans
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
Apple Machine Learning Research
Apple Machine Learning Research
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Proofpoint News Feed
美团技术团队
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
H
Help Net Security
Recent Announcements
Recent Announcements
Microsoft Azure Blog
Microsoft Azure Blog
D
Docker
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
小众软件
小众软件
J
Java Code Geeks
S
SegmentFault 最新的问题
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
The Cloudflare Blog
Recorded Future
Recorded Future
阮一峰的网络日志
阮一峰的网络日志
T
The Blog of Author Tim Ferriss
MyScale Blog
MyScale Blog
Microsoft Security Blog
Microsoft Security Blog

博客园 - 眼里进了砂

Unity Application Block 1.2 学习笔记 [转] Unity Application Block 與 ASP.NET MVC 學習資源整理 [转] 线程之间的通讯---SynchronizationContext [转] .NET2.0中WinForm自定义的程序配置[转] 使用BackgroundWorker组件进行异步操作编程[转] .NET页面事件执行顺序[转] 不用设置iis .net 实现urlrewrite[转] 利用XPath读取Xml文件 javascript中replace与正则表达式 c# BackgroundWorker组件介绍(属性、方法、事件) .net 2.0 BackgroundWorker类详细用法 .NET应用程序中异步调用Web Service的几种方法 come from: veryhappy(wx.net) [转][javascript] Google谷歌首页点点效果 AJAX 中Sys.WebForms.PageRequestManager的事件激发顺序 AJAX 中Sys.Net.WebRequestManager的事件激发顺序 Summary review about Problem.Design.Solution 2 Summary review about Problem.Design.Solution [转发]深入理解 __doPostBack CSS Tips
.Net的线程同步方法一:ManualResetEvent
眼里进了砂 · 2008-07-07 · via 博客园 - 眼里进了砂

.Net的线程同步方法一:ManualResetEvent

class Program
     
{
         
/// <summary>
         
/// ManualResetEvent建立时是把false作为start的初始状态,这个类用于通知另一个线程,让它等待一个或多个线程。
         
/// 如这个例子中,等待线程thread1线程调用mre.WaitOne(), 用于发信号的线程调用mre.Set().
         
/// </summary>

         public static ManualResetEvent mre = new ManualResetEvent(false);

         
public static void trmain()
         
{

             Thread tr 
= Thread.CurrentThread;

             Console.WriteLine(tr.Name 
+ " 开始第一波等待");
            mre.WaitOne();   
//等到什么时候呢?等到mre.Set()被调用
             Console.WriteLine(tr.Name + " 第一波启动t");

            mre.Reset();   
//再次重置
             Console.WriteLine(tr.Name + " 开始第二波等待");
            mre.WaitOne();   
//再次等待
             Console.WriteLine(tr.Name + " 第二波启动");

             
for (int x = 0; x < 10; x++)
             
{

                 Thread.Sleep(
1000);
                 Console.WriteLine(tr.Name 
+ "" + x);
             }

         }
  


         
static void Main(string[] args)
         
{
             Thread thrd1 
= new Thread(new ThreadStart(trmain));
             thrd1.Name 
= "thread1";
             thrd1.Start();

             Thread thrd2 
= new Thread(new ThreadStart(trmain));
             thrd2.Name 
= "thread2";
             thrd2.Start();

             
for (int x = 0; x < 10; x++)
             
{
                 Thread.Sleep(
900);
                 Console.WriteLine(
"Main :" + x);
                 
if (5 == x)
                 
{
                    mre.Set();   
//子线程的mre.WaitOne()可以执行了。第一次等待进程
                     
//;   //如果什么都不做呢,mre.Wait()那个线程就一直等在那里了?
                 }

             }


             
while (thrd1.IsAlive)
             
{

                 Thread.Sleep(
1000);

                 Console.WriteLine(
"Main: waiting for thread to stop");
                mre.Set();   
//第二次通知等待进程
             }



         }

     }

posted @ 2008-07-07 16:03  眼里进了砂  阅读(280)  评论(0)    收藏  举报

刷新页面返回顶部