摘要

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. 生活就像测试, BUG会越来越少,生活会越来越好!

    生活就像测试, BUG会越来越少,生活会越来越好!

  2. Leetcode Perfect Square

    这道题首先想到的算法是DP.每个perfect square number对应的解都是1.先生成一个n+1长的DP list.对于每个i,可以用dp[i] = min(dp[j] + dp[i-j], ...

  3. 【BZOJ-4423】Bytehattan 并查集 + 平面图转对偶图

    4423: [AMPPZ2013]Bytehattan Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 144  Solved: 103[Submit][ ...

  4. bzoj3295: [Cqoi2011]动态逆序对(cdq分治)

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  5. c语言几种异常

    这几天写C程序,问题不断,先记下来吧 double free or corruption 字面意思理解为重复释放空间或崩溃,通常由于你调用了两次free,虽然你可能不是两次给free()传同一个指针, ...

  6. HTTP Header Injection in Python urllib

    catalogue . Overview . The urllib Bug . Attack Scenarios . 其他场景 . 防护/缓解手段 1. Overview Python's built ...

  7. siege详解

    简介 siege是一款HTTP/FTP负载测试和基准压测工具   Download http://download.joedog.org/siege/siege-latest.tar.gz   安装 ...

  8. Aspx生命周期

    今天去面试,碰到面试官问这个问题.aspx页面生命周期是什么?顿时懵逼 还跟我解释:就是页面怎么解析的aspx页面.果断没看过这方面知识,没答上来.现在记录一下 ASP.NET页面生命周期 ASP.N ...

  9. MySql中时间比较的实现

        unix_timestamp 函数可以接受一个参数,也可以不使用参数.它的返回值是一个无符号的整数.不使用参数,它返回自1970年1月1日0时0分0秒到现在所经过的秒数,如果 使用参数,参数的 ...

  10. python函数(五)

    函数 1.函数基本语法及特性 背景提要 现在老板让你写一个监控程序,监控服务器的系统状况,当cpu\memory\disk等指标的使用量超过阀值时即发邮件报警, 你掏空了所有的知识量,写出了以下代码 ...