Thread.Sleep doesn't seem to be supported in .NET for Windows Store apps.
For example, this
System.Threading.Thread.Sleep(1000);
will compile when targeting any .NET Framework (2.0, 3.5, 4.0, 4.5), but not when targeting .NET for Windows Store apps (or in a portable class library which targets both 4.5 and store).
System.Threading.Thread is still there, it just doesn't have the Sleep method.
I need to delay something for a few seconds in my app, is there a suitable replacement?
EDIT why the delay is needed: My app is a game and the delay is to make it look like the computer opponent is "thinking" about his next move. The method is already called asynchronously (main thread isn't blocked), I just want to slow the response time down.
In C#, a Sleep() method temporarily suspends the current execution of the thread for specified milliseconds, so that other threads can get the chance to start the execution, or may get the CPU for execution.
Use Thread. Sleep when you want to block the current thread. Use Task. Delay when you want a logical delay without blocking the current thread.
In c#, the sleep method is useful to suspend or pause the current thread execution for a specified time. We can suspend the thread execution either by passing the time in milliseconds or with TimeSpan property like as shown below. TimeSpan ts = new TimeSpan(0,0,1);
Java Thread Sleep important points For a quiet system, the actual time for sleep is near to the specified sleep time but for a busy system it will be little bit more. Thread sleep doesn't lose any monitors or locks current thread has acquired.
Windows Store apps embrace asynchrony - and an "asynchronous pause" is provided by Task.Delay
. So within an asynchronous method, you'd write:
await Task.Delay(TimeSpan.FromSeconds(30));
... or whatever delay you want. The asynchronous method will continue 30 seconds later, but the thread will not be blocked, just as for all await
expressions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With