ManualResetEvent的Reset问题? 请教大家!
其中的Set明白,WaitOne明白,但是这个Reset就不明白了
过来请教大家拉

解决方案 »

  1.   

    ManualResetEvent 允许线程通过发信号互相通信。通常,此通信涉及一个线程在其他线程进行之前必须完成的任务。当线程开始一个活动(此活动必须在其他线程进行之前完成)时,它调用 Reset 将 ManualResetEvent 设置为非终止状态。此线程可被视为控制 ManualResetEvent。调用 ManualResetEvent 上的 WaitOne 的线程将阻塞,并等待信号。当控制线程完成活动时,它调用 Set 以发出等待线程可以继续进行的信号。并释放所有等待线程。一旦它被终止,ManualResetEvent 将保持终止状态,直到它被手动重置。即对 WaitOne 的调用将立即返回。可以通过将布尔值传递给构造函数来控制 ManualResetEvent 的初始状态,如果初始状态处于终止状态,为 true;否则为 false。ManualResetEvent 还可以和静态(在 Visual Basic 中为 Shared)WaitAll 和 WaitAny 方法一起使用。摘自http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfsystemthreadingmanualreseteventclasstopic.asp
      

  2.   

    "调用 Reset 将 ManualResetEvent 设置为非终止状态"就是这句话不是很明白,
    据我自己理解:
    WaitOne()是等待一个信号,同时阻止当前线程,当某个线程执行Set命令的时候,WaitOne接收这个信号,继续执行。
    当时这个ReSet就不是很理解了,Set是“将事件状态设置为终止状态,允许一个或多个等待线程继续。 ”,而ReSet是“将事件状态设置为非终止状态,导致线程阻止。 ”
    何谓“非终止状态”??“导致阻止线程”中的这个“线程”具体是那个线程?当前线程还是调用waitone的线程呢???
      

  3.   

    using System;
    using System.Threading;namespace ManualReset
    {    class Reset
        {        [STAThread]
            static void Main()
            {
                ManualResetEvent manRE;
                manRE = new ManualResetEvent(true);  // 赋给信号量
                bool state = manRE.WaitOne(1000, true);
                Console.WriteLine("ManualResetEvent After first waitone " + state);            manRE.Reset();               //设置ManualResetEvent状态为无信号量
                state = manRE.WaitOne(5000, true);
                Console.WriteLine("ManualResetEvent After second waitone " + state);            Console.Read();
            }
        }
    }
    using System;
    using System.Threading;
    namespace ManualSet
    {    class Set
        {        [STAThread]
            static void Main(string[] args)
            {
                ManualResetEvent manRE;
                manRE = new ManualResetEvent(false);
                Console.WriteLine("Before waitone");
                bool state = manRE.WaitOne(5000, true);
                Console.WriteLine("ManualResetEvent After first waitone " + state);            manRE.Set();          //将其状态设为有信号量
                Thread.Sleep(3000);
                state = manRE.WaitOne(5000, true);
                Console.WriteLine("ManualResetEvent After second waitone " + state);
                Console.Read();
            }
        }
    }