首页 > 其他分享 >ManualResetEvent

ManualResetEvent

时间:2023-09-24 23:45:10浏览次数:48  
标签:调用 Thread ManualResetEvent 线程 Enter WaitOne

表示线程同步事件,收到信号时,必须手动重置该事件。 此类不能被继承。

 

 该示例以 ManualResetEvent 处于未对齐状态的 开头, (即 传递到 false 构造函数) 。 该示例创建三个线程,其中每个线程通过调用 其 WaitOne 方法在 上ManualResetEvent阻止。 当用户按 Enter 键时,该示例将调用ManualResetEvent 的 Set 方法,该方法释放所有全部三个线程。 将此与 类的行为进行对比,类的行为 AutoResetEvent 一次释放一个线程,并在每次发布后自动重置。

再次按 Enter 键表明 , ManualResetEvent 在调用其 Reset 方法之前,将保持信号状态:此示例再启动两个线程。 这些线程在调用 WaitOne 方法时不会阻止,而是运行到完成。

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

 

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();

        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和 EventWaitHandle 用于线程交互 (或线程信号) 。 有关详细信息,请参阅同步基元概述一文的线程交互或信号部分。

当线程开始必须在其他线程继续之前完成的活动时,它会调用 ManualResetEvent.Reset 以进入 ManualResetEvent 非信号状态。 可以将此线程视为控制 ManualResetEvent。 调用 ManualResetEvent.WaitOne 块的线程正在等待信号。 当控制线程完成活动时,它会调用 ManualResetEvent.Set 来指示等待的线程可以继续。 释放所有等待的线程。

一旦发出信号,将保持信号状态, ManualResetEvent 直到通过调用 Reset() 方法手动重置。 也就是说,调用 立即 WaitOne 返回。

可以通过将布尔值传递给构造函数来控制 的初始状态 ManualResetEvent : true 如果发出初始状态信号, false 则为 ;否则为 。

ManualResetEvent还可以与 和 方法一起使用staticWaitAllWaitAny

从 .NET Framework 版本 2.0 开始,ManualResetEvent派生自 EventWaitHandle 类。 在 ManualResetEvent 功能上等效于 EventWaitHandle 使用 EventResetMode.ManualReset创建的 。

 备注

ManualResetEvent与 类不同,类EventWaitHandle提供对命名系统同步事件的访问权限。

从 .NET Framework 版本 4.0 开始,System.Threading.ManualResetEventSlim类是 的轻型替代项ManualResetEvent

 

标签:调用,Thread,ManualResetEvent,线程,Enter,WaitOne
From: https://www.cnblogs.com/JohnnyLei/p/17726956.html

相关文章