C# System.Threading.AutoResetEvent
表示线程同步事件在一个等待线程释放后收到信号时自动重置.
using System;
using System.Threading; // Visual Studio: Replace the default class in a Console project with
// the following class.
class Example
{
private static AutoResetEvent event_1 = new AutoResetEvent(true);
private static AutoResetEvent event_2 = new AutoResetEvent(false); static void Main()
{
Console.WriteLine("Press Enter to create three threads and start them.\r\n" +
"The threads wait on AutoResetEvent #1, which was created\r\n" +
"in the signaled state, so the first thread is released.\r\n" +
"This puts AutoResetEvent #1 into the unsignaled state.");
Console.ReadLine(); for (int i = ; i < ; i++)
{
Thread t = new Thread(ThreadProc);
t.Name = "Thread_" + i;
t.Start();
}
Thread.Sleep(); for (int i = ; i < ; i++)
{
Console.WriteLine("Press Enter to release another thread.");
Console.ReadLine();
event_1.Set();
Thread.Sleep();
} Console.WriteLine("\r\nAll threads are now waiting on AutoResetEvent #2.");
for (int i = ; i < ; i++)
{
Console.WriteLine("Press Enter to release a thread.");
Console.ReadLine();
event_2.Set();
Thread.Sleep();
} // Visual Studio: Uncomment the following line.
//Console.Readline();
} static void ThreadProc()
{
string name = Thread.CurrentThread.Name; Console.WriteLine("{0} waits on AutoResetEvent #1.", name);
event_1.WaitOne();
Console.WriteLine("{0} is released from AutoResetEvent #1.", name); Console.WriteLine("{0} waits on AutoResetEvent #2.", name);
event_2.WaitOne();
Console.WriteLine("{0} is released from AutoResetEvent #2.", name); Console.WriteLine("{0} ends.", name);
}
} /* This example produces output similar to the following: Press Enter to create three threads and start them.
The threads wait on AutoResetEvent #1, which was created
in the signaled state, so the first thread is released.
This puts AutoResetEvent #1 into the unsignaled state. Thread_1 waits on AutoResetEvent #1.
Thread_1 is released from AutoResetEvent #1.
Thread_1 waits on AutoResetEvent #2.
Thread_3 waits on AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #1.
Press Enter to release another thread. Thread_3 is released from AutoResetEvent #1.
Thread_3 waits on AutoResetEvent #2.
Press Enter to release another thread. Thread_2 is released from AutoResetEvent #1.
Thread_2 waits on AutoResetEvent #2. All threads are now waiting on AutoResetEvent #2.
Press Enter to release a thread. Thread_2 is released from AutoResetEvent #2.
Thread_2 ends.
Press Enter to release a thread. Thread_1 is released from AutoResetEvent #2.
Thread_1 ends.
Press Enter to release a thread. Thread_3 is released from AutoResetEvent #2.
Thread_3 ends.
*/
构造函数
| AutoResetEvent(Boolean) |
用一个指示是否将初始状态设置为终止的布尔值初始化 AutoResetEvent 类的新实例。 |
| Close() |
释放由当前 WaitHandle 占用的所有资源。 (Inherited from WaitHandle) |
| CreateObjRef(Type) |
创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。 (Inherited from MarshalByRefObject) |
| Dispose() |
释放 WaitHandle 类的当前实例所使用的所有资源。 (Inherited from WaitHandle) |
| Dispose(Boolean) |
当在派生类中重写时,释放 WaitHandle 使用的非托管资源,并且可选择释放托管资源。 (Inherited from WaitHandle) |
| Equals(Object) |
确定指定的对象是否等于当前对象。 (Inherited from Object) |
| GetHashCode() |
作为默认哈希函数。 (Inherited from Object) |
| GetLifetimeService() |
检索控制此实例的生存期策略的当前生存期服务对象。 (Inherited from MarshalByRefObject) |
| GetType() |
获取当前实例的 Type。 (Inherited from Object) |
| InitializeLifetimeService() |
获取生存期服务对象来控制此实例的生存期策略。 (Inherited from MarshalByRefObject) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。 (Inherited from Object) |
| MemberwiseClone(Boolean) |
创建当前 MarshalByRefObject 对象的浅表副本。 (Inherited from MarshalByRefObject) |
| ToString() |
返回表示当前对象的字符串。 (Inherited from Object) |
| WaitOne() |
阻止当前线程,直到当前 WaitHandle 收到信号。 (Inherited from WaitHandle) |
| WaitOne(Int32) |
阻止当前线程,直到当前 WaitHandle 收到信号,同时使用 32 位带符号整数指定时间间隔(以毫秒为单位)。 (Inherited from WaitHandle) |
| WaitOne(Int32, Boolean) |
阻止当前线程,直到当前的 WaitHandle 收到信号为止,同时使用 32 位带符号整数指定时间间隔,并指定是否在等待之前退出同步域。 (Inherited from WaitHandle) |
| WaitOne(TimeSpan) |
阻止当前线程,直到当前实例收到信号,同时使用 TimeSpan 指定时间间隔。 (Inherited from WaitHandle) |
| WaitOne(TimeSpan, Boolean) |
阻止当前线程,直到当前实例收到信号为止,同时使用 TimeSpan 指定时间间隔,并指定是否在等待之前退出同步域。 (Inherited from WaitHandle) |
| Handle |
获取或设置本机操作系统句柄。 (Inherited from WaitHandle) |
| SafeWaitHandle |
获取或设置本机操作系统句柄。 (Inherited from WaitHandle) |
| IDisposable.Dispose() |
释放由 WaitHandle 使用的所有资源。 (Inherited from WaitHandle) |
| WaitTimeout |
指示在任何等待句柄终止之前 WaitAny(WaitHandle[], Int32, Boolean) 操作已超时。此字段为常数。 (Inherited from WaitHandle) |
C# System.Threading.AutoResetEvent的更多相关文章
- System.Threading.Timer 定时器的用法
System.Threading.Timer 是C# 中的一个定时器,可以定时(不断循环)执行一个任务.它是在线程上执行的,具有很好的安全性.为此 .Net Framework 提供了5个重载的构造 ...
- System.Threading.Timer的使用技巧
转自:http://www.360doc.com/content/11/0812/11/1039473_139824496.shtml# System.Threading.Timer timer = ...
- C# System.Threading.Timer
提供以指定的时间间隔对线程池线程执行方法的机制 using System; using System.Threading; class TimerExample { static void Main( ...
- c# 多线程之-- System.Threading Timer的使用
作用:每隔多久去执行线程里的方法. class ThreadTimerDemo { static void Main(string[] args) { // Create an AutoResetEv ...
- System.Threading
线程:定义为可执行应用程序中的基本执行单元. 应用程序域:一个应用程序内可能有多个线程. 上下文:一个线程可以移动到一个特定的上下文的实体 导入命名空间: //得到正在执行这个方法的线程 Thread ...
- .Net多线程编程—System.Threading.Tasks.Parallel
System.Threading.Tasks.Parallel类提供了Parallel.Invoke,Parallel.For,Parallel.ForEach这三个静态方法. 1 Parallel. ...
- C# System.Threading.Timer 使用方法
public class TimerHelper { System.Threading.Timer timer; public TaskSendMMS tasksendmms { get; set; ...
- C#中的线程四(System.Threading.Thread)
C#中的线程四(System.Threading.Thread) 1.最简单的多线程调用 System.Threading.Thread类构造方法接受一个ThreadStart委托,改委托不带参数,无 ...
- C#错误之 System.Threading.ThreadAbortException:正在中止线程
参考:http://www.cnblogs.com/chendaoyin/archive/2013/06/27/3159211.html 1.开启一个子线程 //开启一个子线程,子线程调用方法 Met ...
随机推荐
- 一、利用Python编写飞机大战游戏-面向对象设计思想
相信大家看到过网上很多关于飞机大战的项目,但是对其中的模块方法,以及使用和游戏工作原理都不了解,看的也是一脸懵逼,根本看不下去.下面我做个详细讲解,在做此游戏需要用到pygame模块,所以这一章先进行 ...
- SolidWorks基础-快速入门
SolidWorks 介绍 SolidWorks 是一款机械设计自动化软件包 用于设计与分析机械结构 SolidWorks主要是工程师表达自己思想的工具 学习 SolidWorks 的方法 找一个基础 ...
- [ONTAK2015]OR-XOR
[ONTAK2015]OR-XOR 题目大意: 一个长度为\(n(n\le5\times10^5)\)的序列\(A(0\le A_i\le10^{18})\),将其分为恰好\(m\)个连续段,设每一段 ...
- [CF49E]Common ancestor
[CF49E]Common ancestor 题目大意: 有两个由小写字母构成的字符串\(S\)和\(T(|S|,|T|\le50)\).另有\(n(n\le50)\)个形如\(a\to bc\)的信 ...
- 树形动态规划(树形DP)入门问题—初探 & 训练
树形DP入门 poj 2342 Anniversary party 先来个题入门一下~ 题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上 ...
- Java 接口 Comparable
compareTo方法是Comparable接口中唯一的方法.类实现了该接口后表明它的实例具有内在的排序关系.当该对象小于.等于或大于指定对象的时候,分别返回一个负整数.0或者正整数.如果由于指定对象 ...
- Java中map集合系列原理剖析
看了下JAVA里面有HashMap.Hashtable.HashSet三种hash集合的实现源码,这里总结下,理解错误的地方还望指正 HashMap和Hashtable的区别 HashSet和Hash ...
- 1. cs231n k近邻和线性分类器 Image Classification
第一节课大部分都是废话.第二节课的前面也都是废话. First classifier: Nearest Neighbor Classifier 在一定时间,我记住了输入的所有的图片.在再次输入一个图片 ...
- learning to generate question headlines 讲座
渣排版预警! 出发点 新闻用户为什么会点: 主观:用户兴趣/热点事件 客观:新闻标题(新闻入口)/新闻内容(更简单,更有趣) 标题分类: surprise,好奇,负例,数字,你,客观的描述,问题的形式 ...
- Python基础-列表、元祖、字典、字符串
列表和分组 序列概览: 数据结构是通过某种方式组织在一起的数据元素的集合.这些元素可以是数字.字符,甚至可以是其他数据结构. 在python中,最基本的数据结构是序列(sequence). 序列中的每 ...