简体   繁体   English

Thread.Sleep会影响ThreadState吗?

[英]Does Thread.Sleep affect ThreadState?

If I create and start a thread, how does calling Thread.Sleep(x) within that thread affect the ThreadState (if at all)? 如果我创建并启动一个线程,那么在该线程中调用Thread.Sleep(x)如何影响ThreadState(如果有的话)?

Thanks! 谢谢!

From MSDN 来自MSDN

WaitSleepJoin The thread is blocked. WaitSleepJoin线程被阻止。 This could be the result of calling Thread.Sleep or Thread.Join, of requesting a lock — for example, by calling Monitor.Enter or Monitor.Wait — or of waiting on a thread synchronization object such as ManualResetEvent. 这可能是调用Thread.Sleep或Thread.Join,请求锁定的结果 - 例如,通过调用Monitor.Enter或Monitor.Wait - 或等待线程同步对象(如ManualResetEvent)。

Short answer is: Yes! 简短的回答是:是的!

ThreadState defines a set of all possible execution states for threads. ThreadState为线程定义了一组所有可能的执行状态。 Once a thread is created, it is in at least one of the states until it terminates. 一旦创建了一个线程,它就会处于至少一个状态,直到它终止。 Threads created within the common language runtime are initially in the Unstarted state, while external threads that come into the runtime are already in the Running state. 在公共语言运行库中创建的线程最初处于Unstarted状态,而进入运行时的外部线程已处于Running状态。 An Unstarted thread is transitioned into the Running state by calling Start. 通过调用Start,Unstarted线程将转换为Running状态。 Not all combinations of ThreadState values are valid; 并非所有ThreadState值组合都有效; for example, a thread cannot be in both the Aborted and Unstarted states. 例如,一个线程不能同时处于Aborted和Unstarted状态。

Important : Thread state is only of interest in a few debugging scenarios. 重要提示 :线程状态仅在少数调试方案中引起关注。 Your code should never use thread state to synchronize the activities of threads. 您的代码永远不应该使用线程状态来同步线程的活动。

ThreadState: WaitSleepJoin : The thread is blocked as a result of a call to Wait, Sleep, or Join. ThreadState: WaitSleepJoin :由于调用Wait,Sleep或Join,线程被阻塞。

From here . 这里开始

I don't want this to come over as a sarcastic answer, as that won't help anyone - so please take this in the spirit that it was intended. 我不希望这是一个讽刺性的答案,因为这对任何人都没有帮助 - 所以请以本来应有的精神来接受这个。

Have you tried creating a simple winform app with buttons to start, stop and sleep a thread and a status area to show the value of thread.ThreadState? 您是否尝试创建一个简单的winform应用程序,其中包含用于启动,停止和睡眠线程的按钮以及用于显示thread.ThreadState值的状态区域?

This will answer your question. 这将回答你的问题。

If you're going to down vote this at least explain why. 如果你打算投票,至少可以解释原因。 All I'm trying to suggest is that people do a bit of experimentation before posting questions. 我试图建议的是人们在发布问题之前做了一些实验。 If their experiments don't answer their questions, they can ask here with evidence as to what they've already tried. 如果他们的实验没有回答他们的问题,他们可以在这里询问他们已经尝试过的证据。

它转换为WaitSleepJoin

The thread should be put into ThreadState.WaitSleepJoin. 该线程应该放入ThreadState.WaitSleepJoin中。

For details, see ThreadState's Documentation , specifically: 有关详细信息,请参阅ThreadState的文档 ,具体如下:

WaitSleepJoin: "The thread is blocked. This could be the result of calling Thread..::.Sleep or Thread..::.Join, of requesting a lock — for example, by calling Monitor..::.Enter or Monitor..::.Wait — or of waiting on a thread synchronization object such as ManualResetEvent. " WaitSleepJoin:“线程被阻塞。这可能是调用Thread .. ::。Sleep或Thread .. ::。Join,请求锁定的结果 - 例如,通过调用Monitor .. ::。Enter或Monitor .. ::。等待 - 或等待线程同步对象,如ManualResetEvent。“

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

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