什么是背后的WaitHandle的基本概念? [英] What is the basic concept behind WaitHandle?

查看:201
本文介绍了什么是背后的WaitHandle的基本概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是用C#.net线程背后的WaitHandle 的基本概念? 什么是它的使用? 何时使用? 什么是使用的为WaitAll 了WaitAny 里面的方法?

What is the basic concept behind WaitHandle in C# .net threading? Whats is its use? When to use it? What is the use of WaitAll and WaitAny methods inside it?

推荐答案

每当你想控制你的应用程序多线程的执行。虽然这并不仅仅意味着只有一个线程递增计数器;但让线程启动/停止或暂停在事件。

Whenever you would want to control the execution of multiple threads in your app. Though this does not only mean that only one thread increments the counter; but let the threads start/stop or pause upon an event.

请参阅 WaitHandles - 自动/ ManualResetEvent的互斥

- 编辑 -

的WaitHandle 是对的机制的你用来控制线程的执行。它不是处理不是一个线程中访问;其有关使用它们的线程中。

WaitHandles are the mechanism that you "use" to control the threads' execution. Its not about handles not being accessible within a thread; its about using them within the thread.

这可能是一个胖的例子,但请原谅我。想想,一个女人给了五种不同的调子哨子五个女孩,并告诉他们,只要吹的东西会发生哨子;这个过程是每个女孩吹一个口哨,和夫人会知道谁吹响了哨子。

This may be a fat example, but please bear with me; think about, a lady gives five different toned whistles to five girls, and tells them to blow the whistle whenever something would happen; the process is for each girl to blow a whistle, and the lady would know who blew the whistle.

现在,它不是彼此,其对共享的非口哨声,可能是小姐,使用它们来控制的执行或程序中如何女孩会吹哨子。

Now, its not about sharing-the-whistles with each other, its about, probably for the lady, to use them to "control" the execution or the process how girls would blow the whistle.

因此​​,技术上的过程将是:

Therefore, technically the process would be to:

  1. 创建一个等待事件(ManualResetEvent的对象)
  2. 注册事件, WaitHandle.WaitAny(事件);
  3. 在你在你的线程中完成执行操作时,。设置(),它会告诉WaitHandle的说我做的!。
  1. Create a wait event(ManualResetEvent object)
  2. Register the events, WaitHandle.WaitAny(events);
  3. After you are done performing operation in your thread, the .Set(), which would tell the WaitHandle that 'I am done!'.

有关例如,考虑从提供的链接的例子。我已经加入的步骤让你明白的逻辑。这些都不是硬codeD的步骤,但只要你会明白的。

For instance, consider the example from the link provided. I have added the steps for you to understand the logic. These aren't the hardcoded steps, but just so you may understand.

class Test
{
    static void Main()
    {
    //STEP 1: Create a wait handle
        ManualResetEvent[] events = new ManualResetEvent[10];//Create a wait handle
        for (int i=0; i < events.Length; i++)
        {
            events[i] = new ManualResetEvent(false);
            Runner r = new Runner(events[i], i); 
            new Thread(new ThreadStart(r.Run)).Start();
        }

    //STEP 2: Register for the events to wait for
        int index = WaitHandle.WaitAny(events); //wait here for any event and print following line.

        Console.WriteLine ("***** The winner is {0} *****", 
                           index);

        WaitHandle.WaitAll(events); //Wait for all of the threads to finish, that is, to call their cooresponding `.Set()` method.

        Console.WriteLine ("All finished!");
    }
}


class Runner
{
    static readonly object rngLock = new object();
    static Random rng = new Random();

    ManualResetEvent ev;
    int id;

    internal Runner (ManualResetEvent ev, int id)
    {
        this.ev = ev;//Wait handle associated to each object, thread in this case.
        this.id = id;
    }

    internal void Run()
    {
    //STEP 3: Do some work
        for (int i=0; i < 10; i++)
        {
            int sleepTime;
            // Not sure about the thread safety of Random...
            lock (rngLock)
            {
                sleepTime = rng.Next(2000);
            }
            Thread.Sleep(sleepTime);
            Console.WriteLine ("Runner {0} at stage {1}",
                               id, i);
        }

    //STEP 4: Im done!
        ev.Set();
    }
}

这篇关于什么是背后的WaitHandle的基本概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆