Reputation: 634
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 Handle another connection while the first one waits for data.
Upvotes: 1
Views: 1095
Reputation: 1499770
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.
Upvotes: 2