Thread.Sleep replacement in .NET for Windows Store

C#.NetWindows StoreWindows Store-Apps

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

Hate to state the obvious but in case anybody wanted a single line System.Threading.Tasks.Task.Delay(3000).Wait()

Solution 3 - C#

I just had the same problem and found another interesting solution that I wanted to share with you. If you really want to block the thread I would do it like this (thanks @Brannon for the "slim" hint):

// `waitHandle.Set` is never called, so we wait always until the timeout occurs
using (var waitHandle = new ManualResetEventSlim(initialState: false))
{
    waitHandle.Wait(TimeSpan.FromSeconds(5));
}

Solution 4 - C#

MainPage.xaml.cs

public MainPage()
{
  this.InitializeComponent();
  this.WaitForFiveSeconds();
}

private async void WaitForFiveSeconds()
{
  await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(5));
  // do something after 5 seconds!
}

Solution 5 - C#

There is almost NO reason (except for testing purposes) to EVER use Thread.Sleep().

IF (and only if) you have a very good reason to send a thread to sleep, you might want to check Task.Delay() , which you can await to "wait" for a specified time. Though it's never a good idea to have a thread sitting around and do nothing. Bad practise ...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaxView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#Antony ThomasView Answer on Stackoverflow
Solution 3 - C#Johannes EggerView Answer on Stackoverflow
Solution 4 - C#Benny NeugebauerView Answer on Stackoverflow
Solution 5 - C#igrimpeView Answer on Stackoverflow