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 ...
随机推荐
- javaNIO的总结
放大1.5倍查看 使用NIO对文件进行COPY操作 public class TestNIOCopyFile { public static void main(String[] args) thro ...
- TF:Tensorflor之session会话的使用,定义两个矩阵,两种方法输出2个矩阵相乘的结果—Jason niu
import tensorflow as tf matrix1 = tf.constant([[3, 20]]) matrix2 = tf.constant([[6], [100]]) product ...
- XX-NET史上最详细完整教程
转 https://www.cnblogs.com/phperkang/p/8780123.html 前言 XX-NET,系GAE类代理,即通过可用Google ip连接Google App Eng ...
- 使用zabbix监控oracle表空间
0.概述 zabbix是一款极其强大的开源监控工具,下面我分享下zabbix如何监控表空间,跟着这个思路,监控其他项都是类似操作. 前提条件是你已经有了zabbix server和zabbix age ...
- Jmeter压测过程报错the target server failed to respond
失败事务报错信息如下, Socket closed Non HTTP response code: org.apache.http.NoHttpResponseException (the targe ...
- 大数据环境完全分布式搭建linux(centos)中安装zookeeper
切记 要关闭防火墙 chkconfig iptables off(关闭防火墙的命令) 1.解压安装包 tar -zxvf zookeeper-3.4.5.tar.gz 2.在conf文件夹下 修改 ...
- CF871D Paths
link 题意: n个点的无向图,若$\gcd(x,y) \neq1$则$(x,y)$有边,统计$1\sim n$构成的无向图两两点对最短路是之和是多少(两点不连通最短路记为0)?$n\leq ...
- IIS 日志分析工具:Log Parser Studio
1.安装Log Parser,下载地址:http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=24659 ...
- PAT Basic 1006
1006 换个格式输出整数 (15 分) 让我们用字母 B 来表示“百”.字母 S 表示“十”,用 12...n 来表示不为零的个位数字 n(<10),换个格式来输出任一个不超过 3 位的正整数 ...
- Java基础知识总结--String、StringBuffer、StringBuilder
1.Java String 类 String类是final类,也即意味着String类不能被继承,并且它的成员方法都默认为final方法.在Java中,被final修饰的类是不允许被继承的,并且该类中 ...