开发者

Threading.Sleep with System.Threading.Tasks

what happens if i put a System.Threading.Thread.Sleep() into a Task that is started with TaskFactory.StartNew()? Does it put the executing Thread itself to sleep or will this Thread execute another Task while the first one Sleeps?

I`m asking this because I´m currently working on a Server which must sometimes wait to receive data from its clients, and if I put the Task asleep, which is handling the client-connection, I would like to know if the Thread that executes this specific Task is then able to Hand开发者_Go百科le another connection while the first one waits for data.


Thread.Sleep always make the currently executed thread sleep, whether that thread is executing a task or not.

There's no concept of a task sleeping - only a thread. Of course the thread pool will allow multiple threads to execute, so you'll probably just end up with more threads than you really need.

It sounds like you may want to wait for the data asynchronously, continuing with the rest of the work for the task when that data is available.

C# 5's async methods will make all of this a lot simpler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜