C# ManualResetEvent用法详解

作者:陌客& 时间:2021-06-11 09:37:14 

ManualResetEvent表示线程同步事件,可以对所有进行等待的线程进行统一管理(收到信号时必须手动重置该事件)

其构造函数为:


public ManualResetEvent (bool initialState);

参数 initialState 表示是否初始化,如果为 true,则将初始状态设置为终止(不阻塞);如果为 false,则将初始状态设置为非终止(阻塞)。

注意:如果其参数设置为true,则ManualResetEvent等待的线程不会阻塞。 如果初始状态为false, 则在Set调用方法之前, 将阻止线程。

它只有两个方法


//将事件状态设置为终止状态,从而允许继续执行一个或多个等待线程。
public bool Set ();

//将事件状态设置为非终止,从而导致线程受阻。
public bool Reset ();

//返回值:操作成功返回true,否则false

讲了这么多,看个例子理解一下


using System;
using System.Threading;

public class Example
{
 // mre is used to block and release threads manually. It is
 // created in the unsignaled state.
 private static ManualResetEvent mre = new ManualResetEvent(false);

static void Main()
 {
   Console.WriteLine("\nStart 3 named threads that block on a ManualResetEvent:\n");

for (int i = 0; i <= 2; i++)
   {
     Thread t = new Thread(ThreadProc);
     t.Name = "Thread_" + i;
     t.Start(); //开始线程
   }

Thread.Sleep(500);
   Console.WriteLine("\nWhen all three threads have started, press Enter to call Set()" +
            "\nto release all the threads.\n");
   Console.ReadLine();

mre.Set();

Thread.Sleep(500);
   Console.WriteLine("\nWhen a ManualResetEvent is signaled, threads that call WaitOne()" +
            "\ndo not block. Press Enter to show this.\n");
   Console.ReadLine();

for (int i = 3; i <= 4; i++)
   {
     Thread t = new Thread(ThreadProc);
     t.Name = "Thread_" + i;
     t.Start();
   }

Thread.Sleep(500);
   Console.WriteLine("\nPress Enter to call Reset(), so that threads once again block" +
            "\nwhen they call WaitOne().\n");
   Console.ReadLine();

mre.Reset();

// Start a thread that waits on the ManualResetEvent.
   Thread t5 = new Thread(ThreadProc);
   t5.Name = "Thread_5";
   t5.Start();

Thread.Sleep(500);
   Console.WriteLine("\nPress Enter to call Set() and conclude the demo.");
   Console.ReadLine();

mre.Set();

// If you run this example in Visual Studio, uncomment the following line:
   //Console.ReadLine();
 }

private static void ThreadProc()
 {
   string name = Thread.CurrentThread.Name;

Console.WriteLine(name + " starts and calls mre.WaitOne()");

mre.WaitOne(); //阻塞线程,直到调用Set方法才能继续执行

Console.WriteLine(name + " ends.");
 }
}

结果如下

C# ManualResetEvent用法详解

过程:

该示例以信号状态ManualResetEvent( false即传递给构造函数的) 开头。 三个线程, 每个线程在调用其WaitOne方法时被阻止。 当用户按Enter键时, 该示例调用Set方法, 该方法释放所有三个线程,使其继续执行。

再次按 " enter " 键, 此时ManualResetEvent在调用Reset方法之前, 一直保持终止状态,因此这些线程在调用WaitOne方法时不会被阻止, 而是运行到完成。即对应上述(收到信号时必须手动重置该事件)

再次按enter键将导致该示例调用Reset方法, 并启动一个线程, 该线程在调用WaitOne时将被阻止。 按enter键, 最后一次调用Set以释放最后一个线程, 程序结束。

如果还不是很清楚可以进行单步调试,这样就能明白了!

文章参考MSDN:ManualResetEvent Class

来源:https://www.cnblogs.com/forever-Ys/p/11675953.html

标签:C#,ManualResetEvent
0
投稿

猜你喜欢

  • 详解SpringCloud的负载均衡

    2022-03-14 03:42:28
  • Java(TM) Platform SE binary 打开jar文件的操作

    2021-10-02 00:08:12
  • Java享元设计模式优化对象创建提高性能和效率

    2022-05-26 23:12:02
  • java如何删除以逗号隔开的字符串中某一个值

    2023-06-12 11:59:19
  • Unity实现简单虚拟摇杆

    2023-08-04 17:33:09
  • java 自动生成略缩图示例代码

    2021-11-11 01:27:37
  • 基于WPF实现简单放大镜效果

    2022-02-15 23:19:12
  • Java实现角色扮演游戏的示例代码

    2023-03-31 19:41:45
  • java开发实现订阅到货通知帮我们买到想买的东西

    2022-05-01 18:55:11
  • MyBatis在SQL语句中如何获取list的大小

    2021-08-15 12:09:55
  • c语言10个经典小程序

    2023-11-03 01:11:35
  • java 字浮串提取方法汇集

    2023-11-24 14:43:16
  • Java反射概念与使用实例代码

    2022-02-24 23:55:40
  • 关于java数组与字符串相互转换的问题

    2021-08-08 18:50:31
  • WCF和Remoting之间的消息传输

    2023-04-15 01:01:20
  • java开发之Jdbc分页源码详解

    2021-10-28 16:06:48
  • Eclipse转Itellij IDEA导入Git/svn本地项目的详细步骤

    2021-11-06 12:57:10
  • 关于Controller层和Service层的类报错问题及解决方案

    2023-09-28 15:12:23
  • Android中使用Bitmap类将矩形图片转为圆形的方法

    2022-01-04 18:47:40
  • 最优雅地整合 Spring & Spring MVC & MyBatis 搭建 Java 企业级应用(附源码)

    2023-09-26 18:27:57
  • asp之家 软件编程 m.aspxhome.com