什么是正确的方式来等待一个.NET线程启动? [英] What's the proper way to wait for a .NET thread to start up?

查看:238
本文介绍了什么是正确的方式来等待一个.NET线程启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读他们的线程教程内就微软的网站如下:

I was reading the following on Microsoft's website within their threading tutorial:

http://msdn.microsoft.com/en-us/library/aa645740(v = vs.71)的.aspx

MSDN建议使用下面的等待线程变得鲜活起来:

MSDN suggests using the following to wait for a thread to become alive:

while (!oThread.IsAlive);

这是推荐的方式来等待?它会更好插入一个调用DoEvents调用允许主线程保持响应?应该如何处理错误的情况下,进行不顺心的事,线程永远不会到来还活着吗?如果这是不好的做法,当最好的IsAlive使用?

Is this the recommended way to wait? Would it be better to insert a "DoEvents" call to allow the main thread to remain responsive? How should error handling be done in case something goes wrong and the thread never comes "alive?" If this is bad practice, when is IsAlive best used?

几个版主标志着这是一个重复的,但它们指的是另一个问题没有提及任何的IsAlive ,的DoEvents,或适当的错误如果线程启动失败处理。我的问题是具体涉及到MSDN文章我挂在我的问题和建议,用一个繁忙的循环使用的IsAlive。

A few moderators have marked this as a duplicate, but the other question they reference doesn't mention anything about IsAlive, DoEvents, or proper error handling if the thread fails to start. My question is specifically related to the MSDN article I linked to within my question and its suggestion to use a busy loop with IsAlive.

推荐答案

看到,因为没有官方的答案了......你已经在MSDN上看到什么是从2003年,这是比较老技术。对于你想做的事我会使用任务并行库(TPL)。这需要.NET Framwork 4.0 +

Seeing as there is no official answer yet... What you have seen on MSDN is from 2003, this is relatively old technology. For what you want to do I would use the Task Parallel Library (TPL). This requires .NET Framwork 4.0+.

有关如何使用第三方物流为您所需要的看到一个伟大的教程:

For a great tutorial on how to use the TPL for what you require see:


  1. 任务N个并联 1。

J.阿尔巴哈利的线程在C#中

一旦你得到认真处理TPL,随时回来寻问的具体问题。

Once you get to grips with TPL, feel free to come back and look for ask particular questions.

我希望这有助于。

这篇关于什么是正确的方式来等待一个.NET线程启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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