摘要

AutoResetEvent:msdn的描述是通知正在等待的线程已发生事件。此类不能被继承。也就是说它有那么一个时间点,会通知正在等待的线程可以做其它的事情了。

AutoResetEvent

该类有一个带bool类型参数的构造函数

#region Assembly mscorlib.dll, v4.0.0.0
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll
#endregion using System;
using System.Runtime.InteropServices; namespace System.Threading
{
// Summary:
// Notifies a waiting thread that an event has occurred. This class cannot be
// inherited.
[ComVisible(true)]
public sealed class AutoResetEvent : EventWaitHandle
{
// Summary:
// Initializes a new instance of the System.Threading.AutoResetEvent class with
// a Boolean value indicating whether to set the initial state to signaled.
//
// Parameters:
// initialState:
// true to set the initial state to signaled; false to set the initial state
// to non-signaled.
public AutoResetEvent(bool initialState);
}
}

该bool值指示初始化的时候是否设置为终止状态。

AutoResetEvent allows threads to communicate with each other by signaling.Typically, you use this class when threads need exclusive access to a resource.

AutoResetEvent允许线程之间通过信号进行通信。通常,当线程独占访问资源的时候你可以使用该类。

注意

This type implements the IDisposable interface.When you have finished using the type, you should dispose of it either directly or indirectly.To dispose of the type directly, call its Dispose method in a try/catch block.

该类实现了IDisposable接口。当你使用结束的时候,你需要直接或者间接的释放它。直接释放可以通过在try/catch块中调用它的Dispose方法。

一个线程等待在AutoResetEvent上调用WaitOne信号。如果AutoResetEvent为未触发状态,则线程会被阻止,并等待当前控制资源的线程通过调用Set来通知资源可用。

调用Set信号,AutoResetEvent将释放一个等待中的线程。当AutoResetEvent被设置为已触发状态时,它将一直保持已触发状态直到一个等待的线程被激活,然后它将自动编程未触发状态。如果没有任何线程在等待,则状态将无限期地保持为已触发状态。

如果当AutoResetEvent为已触发状态时调用WaitOne,则线程不会被阻止。AutoResetEvent将立即释放线程并返回到未触发状态。

There is no guarantee that every call to the Set method will release a thread.If two calls are too close together, so that the second call occurs before a thread has been released, only one thread is released.It is as if the second call did not happen.Also, if Set is called when there are no threads waiting and the AutoResetEvent is already signaled, the call has no effect.

也不能保证每次调用set方法就释放一个线程。如果两次调用太近,以至于第二次调用在释放线程时,只有一个线程被释放。就好像第二次调用没发生一样。另外,如果设置为当没有线程等待和AutoResetEvent已经发出信号,则调用没有影响。

你可以通过构造函数的bool值参数控制初始化时AutoRestEvent的状态。若要将初始状态设置为终止,则为 true;若要将初始状态设置为非终止,则为 false。

示例

下面的示例将演示如何使用AutoResetEvent通过Set方法在用户按下回车键的时候释放一个线程。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace Wolfy.AutoResetEventDemo
{ class Program
{
const int numIterations = ;
//初始化为非终止状态。
static AutoResetEvent myResetEvent = new AutoResetEvent(false);
static int number; static void Main(string[] args)
{
//创建并启动读线程
Thread myReaderThread = new Thread(new ThreadStart(MyReaderThreadProc));
myReaderThread.Name = "ReaderThread";
myReaderThread.Start();
for (int i = ; i < numIterations; i++)
{
Console.WriteLine("Writer thread writing value:{0}", i);
number = i;
//给读线程信号
myResetEvent.Set();
Thread.Sleep();
}
myReaderThread.Abort();
} private static void MyReaderThreadProc()
{
while (true)
{
myResetEvent.WaitOne();
Console.WriteLine("{0} reading value:{1}", Thread.CurrentThread.Name, number);
}
}
}
}

执行顺序

运行结果

[c#基础]AutoResetEvent的更多相关文章

  1. AutoResetEvent和ManualResetEvent

    本文在于巩固基础 AutoResetEvent 概念:通知正在等待的线程已发生的事件 如果AutoResetEvent 为非终止状态,则线程会被阻止,并等待当前控制资源的线程通过调用 Set 来通知资 ...

  2. C#编程总结(二)多线程基础

    C#编程总结(二)多线程基础 无论您是为具有单个处理器的计算机还是为具有多个处理器的计算机进行开发,您都希望应用程序为用户提供最好的响应性能,即使应用程序当前正在完成其他工作.要使应用程序能够快速响应 ...

  3. C#中的多线程 - 同步基础

    原文:http://www.albahari.com/threading/part2.aspx 文章来源:http://blog.gkarch.com/threading/part2.html 1同步 ...

  4. C#线程池基础

    池(Pool)是一个很常见的提高性能的方式.比如线程池连接池等,之所以有这些池是因 为线程和数据库连接的创建和关闭是一种比较昂贵的行为.对于这种昂贵的资源我们往往会考虑在一个池容器中放置一些资源,在用 ...

  5. .net中的线程同步基础(搬运自CLR via C#)

    线程安全 此类型的所有公共静态(Visual Basic 中为 Shared)成员对多线程操作而言都是安全的.但不保证任何实例成员是线程安全的. 在MSDN上经常会看到这样一句话.表示如果程序中有n个 ...

  6. C#中的多线程 - 同步基础 z

    原文:http://www.albahari.com/threading/part2.aspx 专题:C#中的多线程 1同步概要Permalink 在第 1 部分:基础知识中,我们描述了如何在线程上启 ...

  7. Python GUI之tkinter窗口视窗教程大集合(看这篇就够了) JAVA日志的前世今生 .NET MVC采用SignalR更新在线用户数 C#多线程编程系列(五)- 使用任务并行库 C#多线程编程系列(三)- 线程同步 C#多线程编程系列(二)- 线程基础 C#多线程编程系列(一)- 简介

    Python GUI之tkinter窗口视窗教程大集合(看这篇就够了) 一.前言 由于本篇文章较长,所以下面给出内容目录方便跳转阅读,当然也可以用博客页面最右侧的文章目录导航栏进行跳转查阅. 一.前言 ...

  8. WPF MVVM UI分离之《交互与数据分离》 基础才是重中之重~delegate里的Invoke和BeginInvoke 将不确定变为确定系列~目录(“机器最能证明一切”) 爱上MVC3系列~全局异常处理与异常日志 基础才是重中之重~lock和monitor的区别 将不确定变成确定~我想监视我的对象,如果是某个值,就叫另一些方法自动运行 将不确定变成确定~LINQ DBML模型可以对

    WPF MVVM UI分离之<交互与数据分离>   在我们使用WPF过程中,不可避免并且超级喜欢使用MVVM框架. 那么,使用MVVM的出发点是视觉与业务逻辑分离,即UI与数据分离 诸如下 ...

  9. Redis总结(五)缓存雪崩和缓存穿透等问题 Web API系列(三)统一异常处理 C#总结(一)AutoResetEvent的使用介绍(用AutoResetEvent实现同步) C#总结(二)事件Event 介绍总结 C#总结(三)DataGridView增加全选列 Web API系列(二)接口安全和参数校验 RabbitMQ学习系列(六): RabbitMQ 高可用集群

    Redis总结(五)缓存雪崩和缓存穿透等问题   前面讲过一些redis 缓存的使用和数据持久化.感兴趣的朋友可以看看之前的文章,http://www.cnblogs.com/zhangweizhon ...

随机推荐

  1. css-文本超出后显示省略号

    1.如果是单行文本: overflow: hidden; text-overflow: ellipsis; white-space: nowrap; 2.如果是多行文本,将文本框高度设为文字行高的倍数 ...

  2. IOS开发中UI编写方式——code vs. xib vs.StoryBoard

    最近接触了几个刚入门的iOS学习者,他们之中存在一个普遍和困惑和疑问,就是应该如何制作UI界面.iOS应用是非常重视用户体验的,可以说绝大多数的应用成功与否与交互设计以及UI是否漂亮易用有着非常大的关 ...

  3. VisualSVN Server和Subversion的联系

    VisualSVN Server是只能在Windows平台上搭建的SVN服务器,内核使用的是Subversion,做了整合:apache+subversion+WMI(实现操作界面等). 用这个的好处 ...

  4. Python基础2:流程控制语句 while / for循环

    [ while 循环 ] 如果要计算1+2+3,我们可以直接写表达式: >>> 1 + 2 + 3 要计算1+2+3+...+10,勉强也能写出来. 但是,要计算1+2+3+...+ ...

  5. 洛谷P2246 SAC#1 - Hello World(升级版)

    题目背景 T1答案要mod1000000007(10^9+7),请重新提交,非常抱歉! 一天,智障的pipapi正在看某辣鸡讲义学程序设计. 题目描述 在讲义的某一面,他看见了一篇文章.这篇文章由英文 ...

  6. pack、unpack自制二进制“数据库”

    引言 pack.unpack函数,如果没有接触过socket,这个可能会比较陌生,这两个函数在socket交互的作用是组包,将数据装进一个二进制字符串,和对二进制字符串中的数据进行解包,这个里面有好多 ...

  7. JDBC连接简介

    package jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; ...

  8. 基本概率分布Basic Concept of Probability Distributions 3: Geometric Distribution

    PDF version PMF Suppose that independent trials, each having a probability $p$, $0 < p < 1$, o ...

  9. JSTL的if-else表式

    JSTL用法,这里不细讲了,主要是if-else的写法: 代码片段: <c:choose> <c:when test="${user.role eq 1 }"&g ...

  10. 【Alpha版本】冲刺-Day3

    队伍:606notconnected 会议时间:11月11日 会议总结 张斯巍(433) 今天安排:个人信息界面设计 完成度:100% 明天计划:个人信息界面设计 遇到的问题:ps掌握的还不够熟练,导 ...