windows 单线程上的线程 WaitHandle

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6741540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:29:21  来源:igfitidea点击:

Thread WaitHandle on a Single Thread

c#.netwindowsmultithreadingparallel-processing

提问by Milan Solanki

My code is

我的代码是

public static void Invoke(Action[] Actions)
{
    Thread[] threadArray = new Thread[Actions.Length];
    for (int i = 0; i < Actions.Length; i++)
    {
        threadArray[i] = new Thread(() =>
        {
            Actions[i].Invoke();
        });
        threadArray[i].Start();
    }
}

public static void WaitAll()
{

}

public static void WaitAny()
{

}

I want to wait for all threads to complete, and also get notification when any thread completes,

我想等待所有线程完成,并在任何线程完成时收到通知,

like WaitAny, WaitAll

WaitAnyWaitAll

But waithandles can be used on threadpools only, could not find any examples for use on single thread.

但是等待句柄只能用于线程池,找不到任何用于单线程的示例。

My app requires a lot of threads, hundreds of them, theadpool has max thread limit, rest tasks are queued.

我的应用程序需要很多线程,数百个,adpool 有最大线程限制,其余任务排队。

How do I manage that??

我该如何管理??

UPDATE:here is the code, please let me know if better code possible.

更新:这是代码,如果可能有更好的代码,请告诉我。

public class ParallelV2
{
    static int waitcount = 0;
    static WaitHandle[] waitHandles;

    public static void Invoke(Action[] Actions)
    {
        waitcount = Actions.Length;

        Thread[] threadArray = new Thread[Actions.Length];
        waitHandles = new WaitHandle[Actions.Length];

        for (int i = 0; i < Actions.Length; i++)
        {
            var count = i;

            waitHandles[count] = new AutoResetEvent(false);
            threadArray[count] = new Thread(() =>
            {
                Actions[count].Invoke();
                ((AutoResetEvent)waitHandles[count]).Set();
            });

            threadArray[count].Start();
        }
    }

    public static void WaitAll()
    {
        while (waitcount > 0)
        {
            WaitHandle.WaitAny(waitHandles);
            waitcount--;
        }
    }

    public static void WaitAny()
    {
        WaitHandle.WaitAny(waitHandles);
    }
}

采纳答案by edepperson

In the example below, I had three threads that I needed to track. Each thread, when it was done would set its corresponding handle.

在下面的示例中,我需要跟踪三个线程。每个线程在完成后都会设置其相应的句柄。

Declare it like this:

像这样声明它:

private WaitHandle[] waithandles;  // see comment on static below

Create it like this:

像这样创建它:

waitcount = 3;
waithandles = new WaitHandle[3] { new AutoResetEvent(false), new AutoResetEvent(false), new AutoResetEvent(false) };

In the thread, when its finished,set it like this:

在线程中,完成后,将其设置为:

((AutoResetEvent)waithandles[i]).Set();

(Actually, thats over-simplified, but it will work if you make the waithandle static. What I actually did was have the thread perform a callback at the end of its life to signal the waithandle)

(实际上,这过于简化了,但是如果您使 waithandle 保持静态,它就会起作用。我实际上所做的是让线程在其生命周期结束时执行回调以向 waithandle 发出信号)

In the main thread, check it like this. When the waitcount reached zero, I knew all threads had completed

在主线程中,像这样检查它。当等待计数达到零时,我知道所有线程都已完成

while (waitcount > 0)
{
    WaitHandle.WaitAny(waithandles, 30000);
    waitcount--;
}

回答by MRAB

You can wait for a thread myThreadto complete with myThread.Join().

您可以等待线程myThread完成myThread.Join()