using System;
using System.Threading; public class Example
{
// mre is used to block and release threads manually. It is
// created in the unsignaled state.
private static ManualResetEvent mre = new ManualResetEvent(false); static void Main()
{
Console.WriteLine("\nStart 3 named threads that block on a ManualResetEvent:\n"); for(int i = ; i <= ; i++)
{
Thread t = new Thread(ThreadProc);
t.Name = "Thread_" + i;
t.Start();
} Thread.Sleep();
Console.WriteLine("\nWhen all three threads have started, press Enter to call Set()" +
"\nto release all the threads.\n");
Console.ReadLine(); mre.Set(); Thread.Sleep();
Console.WriteLine("\nWhen a ManualResetEvent is signaled, threads that call WaitOne()" +
"\ndo not block. Press Enter to show this.\n");
Console.ReadLine(); for(int i = ; i <= ; i++)
{
Thread t = new Thread(ThreadProc);
t.Name = "Thread_" + i;
t.Start();
} Thread.Sleep();
Console.WriteLine("\nPress Enter to call Reset(), so that threads once again block" +
"\nwhen they call WaitOne().\n");
Console.ReadLine(); mre.Reset(); // Start a thread that waits on the ManualResetEvent.
Thread t5 = new Thread(ThreadProc);
t5.Name = "Thread_5";
t5.Start(); Thread.Sleep();
Console.WriteLine("\nPress Enter to call Set() and conclude the demo.");
Console.ReadLine(); mre.Set(); // If you run this example in Visual Studio, uncomment the following line:
//Console.ReadLine();
} private static void ThreadProc()
{
string name = Thread.CurrentThread.Name; Console.WriteLine(name + " starts and calls mre.WaitOne()"); mre.WaitOne(); Console.WriteLine(name + " ends.");
}
} /* This example produces output similar to the following: Start 3 named threads that block on a ManualResetEvent: Thread_0 starts and calls mre.WaitOne()
Thread_1 starts and calls mre.WaitOne()
Thread_2 starts and calls mre.WaitOne() When all three threads have started, press Enter to call Set()
to release all the threads. Thread_2 ends.
Thread_0 ends.
Thread_1 ends. When a ManualResetEvent is signaled, threads that call WaitOne()
do not block. Press Enter to show this. Thread_3 starts and calls mre.WaitOne()
Thread_3 ends.
Thread_4 starts and calls mre.WaitOne()
Thread_4 ends. Press Enter to call Reset(), so that threads once again block
when they call WaitOne(). Thread_5 starts and calls mre.WaitOne() Press Enter to call Set() and conclude the demo. Thread_5 ends.
*/

原文来自:http://msdn.microsoft.com/zh-cn/library/system.threading.manualresetevent(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

最新文章

  1. Android--短信
  2. PHP IIS SPY
  3. 运用HBuilder上传到GitHub
  4. 织梦dede标签tags的美化教程
  5. 淘特房产CMS系统 7.5
  6. document.documentElement 和document.body 以及其属性
  7. 关于knob.js进度插件的使用
  8. 我的TDD实践---UnitTest单元测试
  9. iOS开发概述UIkit动力学,讲述UIKit的Dynamic特性,UIkit动力学是UIkit框架中模拟真实世界的一些特性。
  10. java中的final与static
  11. hibernate解读之session--基于最新稳定版5.2.12
  12. RobotFramework下的http接口自动化Get Response header 关键字的使用
  13. 牛客小白月赛13-J小A的数学题 (莫比乌斯反演)
  14. EffectiveC++ 第2章 构造/析构/赋值运算
  15. 干了这杯Java之HashMap
  16. 三十四、Linux 进程与信号——信号特点、信号集和信号屏蔽函数
  17. PHP实现网络Socket及IO多路复用
  18. 2017-2018-2 20165314 实验三《 敏捷开发与XP实践》实验报告
  19. [Android] websocket客户端开发
  20. [Big Data - Kafka] kafka学习笔记:知识点整理

热门文章

  1. 从0开始学Java——从jsp到servlet转换的各种辅助元素介绍
  2. JAVA中获取当前系统时间
  3. [CareerCup] 9.11 Parenthesize the Expression 表达式加括号
  4. Linux10.11-10.18)学习笔记(
  5. 掌握GCD以及后台永久运行的代码 (使用GCD处理后台线程和UI线程的交互)
  6. IOS开发之——意见反馈UITextView的使用+不能输入字符输入
  7. 20145215《Java程序设计》第3周学习总结
  8. java &lt;? super Fruit&gt;与&lt;? extends Fruit&gt;
  9. Mustache.js前端模板引擎源码解读
  10. jQuery UI dialog