How to get the expected Thread.Sleep?

I am writing a network application based on the wait / sleep paradigm.

Sometimes connection errors occur, and in my experience it’s worth waiting a while, and then repeat the operation.

The problem is that if I use Thread.Sleep or another similar blocking operation in await / async, it blocks all activity in the caller's thread.

How to replace Thread.Sleep (10000) with the same effect as

await Thread.SleepAsync(10000) 

?

UPDATE

I prefer an answer that does this without creating an additional topic

+106
multithreading c # async-await
Nov 17 '12 at 10:32
source share
1 answer

Other answers suggesting starting a new stream are a bad idea - there is no need to do this at all. Part of the async / await point is to reduce the number of threads your application requires.

Instead, you should use Task.Delay , which does not require a new thread, and was designed specifically for this purpose:

 // Execution of the async method will continue one second later, but without // blocking. await Task.Delay(1000); 
+257
Nov 17 '12 at 11:28
source share



All Articles