首页 > 其他 > 详细

manualresetevent的用法学习

时间:2014-02-19 00:33:28      阅读:474      评论:0      收藏:0      [点我收藏+]

ManualResetEvent allows threads to communicate with each other by signaling.‘>ManualResetEvent 允许线程通过发信号互相通信。 通常,此通信涉及一个线程在其他线程进行之前必须完成的任务。

Reset to put ManualResetEvent in the non-signaled state.‘>当一个线程开始一个活动(此活动必须完成后,其他线程才能开始)时,它调用 Reset 以将 ManualResetEvent 置于非终止状态。 ManualResetEvent.‘>此线程可被视为控制 ManualResetEventWaitOne on the ManualResetEvent will block, awaiting the signal.‘>调用 ManualResetEvent 上的 WaitOne 的线程将阻止,并等待信号。 Set to signal that the waiting threads can proceed.‘>当控制线程完成活动时,它调用 Set 以发出等待线程可以继续进行的信号。 并释放所有等待线程。

ManualResetEvent remains signaled until it is manually reset.‘>一旦它被终止,ManualResetEvent 将保持终止状态,直到它被手动重置。 WaitOne return immediately.‘>即对 WaitOne 的调用将立即返回。

ManualResetEvent by passing a Boolean value to the constructor, true if the initial state is signaled and false otherwise.‘>可以通过将布尔值传递给构造函数来控制 ManualResetEvent 的初始状态,如果初始状态处于终止状态,为 true;否则为 false

 

ManualResetEvent works.‘>下面的示例说明ManualResetEvent嵌入的方法: ManualResetEvent in the unsignaled state (that is, false is passed to the constructor).‘>该示例以未终止状态中的 ManualResetEvent 开始(即 false 传递给构造函数)。 ManualResetEvent by calling its WaitOne method.‘>该示例创建三个线程,每个线程都可以被 ManualResetEvent 阻止(通过调用其 WaitOne 方法)。 Enter key, the example calls the Set method, which releases all three threads.‘>当按“输入”键时,示例将调用 Set 方法,该方法可释放所有三个线程。 AutoResetEvent class, which releases threads one at a time, resetting automatically after each release.‘>对比该其与 AutoResetEvent 的行为,这释放一次一个线程,在每个释放后自动重新设置。

Enter key again demonstrates that the ManualResetEvent remains in the signaled state until its Reset method is called: The example starts two more threads.‘>再次按“输入”键后,我们会发现 ManualResetEvent在调用其 Reset 方法前会一直保持终止状态:该示例另外启动两个线程。 WaitOne method, but instead run to completion.‘>这些线程不会阻止,如果它们调用 WaitOne方法,而是会运行至完成。

Enter key again causes the example to call the Reset method and to start one more thread, which blocks when it calls WaitOne.‘>再次按“输入”键会使该示例调用 Reset 方法并另外启动一个线程,该线程会在它调用 WaitOne 的时候被阻塞。 Enter key one final time calls Set to release the last thread, and the program ends.‘>最后一次按“输入”键将调用 Set 来释放最后一个线程,然后程序结束。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ManulReseteventTest
{
    class Program
    {
        //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(string[] args)
        {
            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.");
        }
    }
}

manualresetevent的用法学习

原文:http://www.cnblogs.com/sfbrzkh/p/3554181.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!