AutoResetEvent WaitOne和Set使用实例
2011-07-28 15:25
501 查看
/// <summary> /// 一共写100行 /// </summary> private const int maxLineNumber = 100; /// <summary> /// 测试文件路径 /// </summary> private const string Path = @"D:\1.txt"; static void Main(string[] args) { AutoResetEvent autoReset = new AutoResetEvent(false); if (!File.Exists(Path)) { FileStream fs = File.Create(Path); fs.Close(); } ////开启新线程用来读 Thread readThread = new Thread(new ThreadStart(() => { for (int i = 0; i < maxLineNumber; i++) { /////等待写线程Set autoReset.WaitOne(); StreamReader sr = File.OpenText(Path); Console.WriteLine(sr.ReadToEnd()); sr.Close(); } })); readThread.Start(); ////写线程每写一行,释放一个读线程去读取刚写入的文字 for (int i = 0; i < maxLineNumber; i++) { File.WriteAllText(Path, i.ToString() + "\r\n"); autoReset.Set(); Thread.Sleep(1000); } Console.ReadKey(); }
相关文章推荐
- c++ 线程中CreateEvent SetEvent/ResetEvent WaitForSingleObject的使用
- 线程学习笔记(EventWaitHandler)AutoResetEvent的使用
- WaitForMultipleObjects和Event配合使用,限制程序运行一个实例
- 最简实例说明wait、notify、notifyAll的使用方法
- 线程间通讯:WaitHandler使用实例及分析
- 最简实例说明WAIT、NOTIFY、NOTIFYALL的使用方法
- 看点:Math.random()和response.setHeader(String name,String value)的使用实例
- 最简实例说明wait、notify、notifyAll的使用方法
- wait_event_interruptible 使用方法
- CreateEvent/OpenEvent/SetEvent/ResetEvent/WaitForSingleObject 相关用法说明
- Scala编程实例:使用Set和Map
- wait_event_interruptible() 和 wake_up()的使用
- 最简实例说明wait、notify、notifyAll的使用方法
- 使用EventWaitHandle控制线程间的同步
- PHP魔术方法__GET、__SET使用实例
- 使用wait_event_interruptible,何时会被唤醒?
- Java wait() notify()方法使用实例讲解
- 最简实例说明wait、notify、notifyAll的使用方法
- 使用Eclipse开发Android报错:Project has no target set. Edit the project properties to set one.
- CreateEvent/OpenEvent/SetEvent/ResetEvent/WaitForSingleObject 相关用法说明