C#知识点讲解之ManualResetEvent类的使用

定义

ManualResetEvent 类
命名空间:System.Threading
程序集:mscorlib.dll
表示线程同步事件,收到信号时,必须手动重置该事件。 无法继承此类。

用于对于所有线程的控制:

常见使用方法:

1、初始化;

//true-初始状态为发出信号;false-初始状态为未发出信号,一般设置为false,手动来发出信号
 ManualResetEvent mre = new ManualResetEvent(false);

2、阻塞

mre.WaitOne();  //所有线程会被阻塞,直到set后,才执行WaitOne()后的代码

3、解除阻塞

mre.Set();  //当你想要的状态达到了,就接触阻塞,继续执行线程后面的代码
//备注: Set()后,如果再调用WaitOne()是无效的,线程不会被阻塞,必须调用Reset()才行

4、重置

mre.Reset();  //重置,只有这样,当再次执行WaitOne()的时候才会再次被阻塞

最后上一个官方的案例:

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");
		//中文注释1:开启三个线程,每个线程开启后调用WaitOne()阻塞。
        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();
		//中文注释2:只有当Set()后才会执行WaitOne()后的代码
        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();
		//中文注释3:继续再开两个线程,仍然调用WaitOne(),但是不会阻塞,会继续执行
        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();
		//中文注释4:只有Reset()后,下面再开线程就会继续被阻塞
        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();
		//中文注释5:再次Set(),就可以了
        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();

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

/* This example produces output similar to the following:

Start 3 named threads that block on a ManualResetEvent:

Thread_0 starts and calls mre.WaitOne()
Thread_1 starts and calls mre.WaitOne()
Thread_2 starts and calls mre.WaitOne()

When all three threads have started, press Enter to call Set()
to release all the threads.


Thread_2 ends.
Thread_0 ends.
Thread_1 ends.

When a ManualResetEvent is signaled, threads that call WaitOne()
do not block. Press Enter to show this.


Thread_3 starts and calls mre.WaitOne()
Thread_3 ends.
Thread_4 starts and calls mre.WaitOne()
Thread_4 ends.

Press Enter to call Reset(), so that threads once again block
when they call WaitOne().


Thread_5 starts and calls mre.WaitOne()

Press Enter to call Set() and conclude the demo.

Thread_5 ends.
 */

番外:

对比 ManualResetEvent 和 AutoResetEvent

举例:

AutoResetEvent :地铁口刷卡通过,每次刷卡视为一次Set(),每Set()一次,只放开一个线程通过,一般是用来同步访问资源。

ManualResetEvent :景点大门,每次Set()视为打开大门,很多人(线程)都可以进,无论先来的还是后来的。直到Reset()关闭大门。

单独对于ManualResetEvent :

WaitOne():给大门插上插销,谁都不让进
Set():打开大门,谁都可以进,大门打开的时候插插销是没效果的。
Reset():关上大门,但没插插销,后面来的仍然可以进,直到WaitOne(),再插上插销,才不能进

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值