对ManualResetEvent和AutoResetEvent的巩固练习

在多线程编程中,最常用到的就是线程同步问题,前段时间开发地址采集服务器,需要携带经纬度到MapAbc中采集后,返回地址,才可以进行下一条经纬度的采集,因为队列处理和解析不是同一个线程,并且是解析经纬度是异步的操作,所以就涉及到线程同步问题,所以针对这个对ManualResetEvent和AutoResetEvent两个信号量巩固练习一下。

 1 class Program
 2     {
 3         static ManualResetEvent _ManualResetEvent = new ManualResetEvent(false);
 4         static void Main(string[] args)
 5         {
 6             int i = 0;
 7             Thread _Thread = new Thread(TestFun);
 8             _Thread.Start();
 9
10             _ManualResetEvent.Set();
11             //Thread.Sleep(100);
12             Console.WriteLine("test1");
13             Console.ReadLine();
14         }
15
16         static void TestFun()
17         {
18             while (true)
19             {
20                 _ManualResetEvent.WaitOne();
21                 Console.WriteLine("test");
22                 Thread.Sleep(100);
23             }
24         }
25     }

当是手动时,信号量为打开状态,会一直打印test

 1 class Program
 2     {
 3         static ManualResetEvent _ManualResetEvent = new ManualResetEvent(false);
 4         static void Main(string[] args)
 5         {
 6             int i = 0;
 7             Thread _Thread = new Thread(TestFun);
 8             _Thread.Start();
 9
10             _ManualResetEvent.Set();
11             //Thread.Sleep(100);
12             Console.WriteLine("test1");
13             Console.ReadLine();
14         }
15
16         static void TestFun()
17         {
18             while (true)
19             {
20                 _ManualResetEvent.WaitOne();
21                 Console.WriteLine("test");
22                 Thread.Sleep(100);
23                 _ManualResetEvent.Reset();
24             }
25         }
26     }

当把信号量重置后,就不会继续打印test了

如果使用AutoResetEvent的话

 1  class Program
 2     {
 3         static AutoResetEvent _AutoResetEvent = new AutoResetEvent(false);
 4         static void Main(string[] args)
 5         {
 6             int i = 0;
 7             Thread _Thread = new Thread(TestFun);
 8             _Thread.Start();
 9
10             _AutoResetEvent.Set();
11             //Thread.Sleep(100);
12             Console.WriteLine("test1");
13             Console.ReadLine();
14         }
15
16         static void TestFun()
17         {
18             while (true)
19             {
20                 _AutoResetEvent.WaitOne();
21                 Console.WriteLine("test");
22
23             }
24         }
25     }

其实跟ManualResetEvent一个原理,只是Set之后,只执行一次,需要重新再Set,WaitOne处才会继续执行下去

时间: 2024-07-28 21:09:47

对ManualResetEvent和AutoResetEvent的巩固练习的相关文章

C#多线程之二:ManualResetEvent和AutoResetEvent

初次体验 ManualResetEvent和AutoResetEvent主要负责多线程编程中的线程同步:以下一段是引述网上和MSDN的解析: 在.Net多线程编程中,AutoResetEvent和ManualResetEvent这两个类经常用到, 他们的用法很类似,但也有区别.Set方法将信号置为发送状态,Reset方法将信号置为不发送状态,WaitOne等待信号的发送.可以通过构造函数的参数值来决定其初始状态,若为true则非阻塞状态,为false为阻塞状态.如果某个线程调用WaitOne方法

C#中多线程信号控制ManualResetEvent和AutoResetEvent

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; namespace WindowsFormsApplication22 { publ

ManualResetEvent和AutoResetEvent的区别

在讨论这个问题之前,我们先了解这样一种观点,线程之间的通信是通过发信号来进行沟通的.(这不是废话) 先来讨论ManualResetEvent,讨论过程中我会穿插一些AutoResetEvent的内容,来做对比: ManualResetEvent都可以阻塞一个或多个线程,直到收到一个信号告诉ManualResetEvent不要再阻塞当前的线程. 可以想象ManualResetEvent这个对象内部有一个Boolean类型的属性IsRelease来控制是否要阻塞当前线程.这个属性我们在初始化的时候可

C#多线程之 ManualResetEvent和AutoResetEvent

初次体验 ManualResetEvent和AutoResetEvent主要负责多线程编程中的线程同步:以下一段是引述网上和MSDN的解析: 在.Net多线程编程中,AutoResetEvent和ManualResetEvent这两个类经常用到, 他们的用法很类似,但也有区别.Set方法将信号置为发送状态,Reset方法将信号置为不发送状态,WaitOne等待信号的发送.可以通过构造函数的参数值来决定其初始状态,若为true则非阻塞状态,为false为阻塞状态.如果某个线程调用WaitOne方法

【C#】【Thread】ManualResetEvent和AutoResetEvent区别

ManualResetEvent和AutoResetEvent主要用于线程之间同步问题. 主要使用方法有Set();Reset();WaitOne(); Set():将事件状态设置为终止状态,允许一个或多个等待线程继续.也就是说是结束状态,这个时候可以运行. Reset():将事件状态设置为非终止状态,导致线程阻止.也就是说是在运行状态,这个时候WaitOne()位置会等待,阻塞. WaitOne():阻止当前线程,直到当前 System.Threading.WaitHandle 收到信号.就是

211. Orchard学习 二 2、ManualResetEvent 与 AutoResetEvent

一.Orchard里异步请求处理线程队列的控制 Orchard的Orchard.WarmupStarter模块,为HttpApplication.BeginRequest时间附加了一个异步处理事件:BeginBeginRequest. 1: /// <summary> 2: /// 启动 System.Web.HttpApplication.BeginRequest 的异步处理的 System.Web.BeginEventHandler 3: /// System.Web.HttpApplic

服务总结 -多线程 - 线程同步(AutoResetEvent与ManualResetEvent)

前言 在我们编写多线程程序时,会遇到这样一个问题:在一个线程处理的过程中,需要等待另一个线程处理的结果才能继续往下执行.比如:有两个线程,一个用来接收Socket数据,另一个用来处理Socket数据,而处理Socket数据的那个线程需要在接收到Socket数据后才能处理运行,就要等待接收线程接收数据.那么处理线程如何等待,接收线程又如何通知处理线程呢? 其中一个比较好的方式就是使用AutoResetEvent/ManualResetEvent 1. AutoResetEvent/ManualRe

ManualResetEvent &amp; AutoResetEvent

ManualResetEvent和AutoResetEvent的作用可以理解为在线程执行中插入停顿点flag终止程序运行,然后通过设置flag的状态来使得程序继续运行. 两者的区别是:ManualResetEvent设置flag状态为可以运行后,所有在终止点的程序都可以继续运行:AutoResetEvent设置flag状态后,只会有一个程序继续运行(如果AutoResetEvent设置的flag有100个程序在等待,那flag开始状态必须要设置100次才能使得所有的线程都执行完毕) 示例如下(M

线程同步 –AutoResetEvent和ManualResetEvent

上一篇介绍了通过lock关键字和Monitor类型进行线程同步,本篇中就介绍一下通过同步句柄进行线程同步. 在Windows系统中,可以使用内核对象进行线程同步,内核对象由系统创建并维护.内核对象为内核所拥有,所以不同进程可以访问同一个内核对象, 如进程.线程.事件.信号量.互斥量等都是内核对象.其中,信号量,互斥体,事件是Windows专门用来进行线程同步的内核对象. 在.NET中,有一个WaitHandle抽象类,这个类型封装了一个Windows内核对象句柄,在C#代码中,我们就可以使用Wa