繁体   English   中英

C#-线程中的方法未执行

[英]C# - Method in thread is not executed

使用thread.start();不执行线程中的方法; 相反,当我使用thread.join()时,将执行它们的方法。

Thread[] threads = new Thread[12];
        int temp;

        //_stopRequest = false;
        for (int i = 0; i < threads.Length - 1; i++)
        {
            temp = i;
            threads[temp] = new Thread(new ThreadStart(() => test(test1[temp],"start", temp)));
            threads[temp].Start();
            //threads[temp].Join();
        }

任何人都可以阐明这一点。

尝试同时启动所有线程

尝试添加

for(int i=0; i<threads.Length-1; i++)
{
    threads[i].Join();
}

在方法的末尾。 您的线程将同时启动,您将等待它们的结束。 其他变体:将其IsBackground属性设置为true。 因此,在这些线程完成工作之前,您的程序将不会完成。

PS如果要使用闭包,请在循环中使用“ int temp = i;”。 在您的情况下,所有线程都关闭到相同的变量temp。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM