欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# ManualResetEvent用法

程序员文章站 2022-07-04 23:43:14
ManualResetEvent表示线程同步事件,可以对所有进行等待的线程进行统一管理(收到信号时必须手动重置该事件) 其构造函数为: 参数 initialState 表示是否初始化,如果为 true,则将初始状态设置为终止(不阻塞);如果为 false,则将初始状态设置为非终止(阻塞)。 注意:如 ......

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

 

  其构造函数为:

public manualresetevent (bool initialstate);

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

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

 

  它只有两个方法

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

//将事件状态设置为终止状态,从而允许继续执行一个或多个等待线程。
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