99

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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Max
  • 9,220
  • 10
  • 51
  • 83

5 Answers5

203

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Unfortunately, Task.Delay doesn't seem to be supported when targeting .NET 4.5 + store + WP7 in a portable class library.. I guess I'll have move this into the platform specific classes. – Max Sep 28 '12 at 14:13
  • 1
    @Max: No, because it didn't exist before .NET 4.5. IIRC, WP7 itself doesn't have any TPL support. (I could be wrong...) – Jon Skeet Sep 28 '12 at 14:14
  • 4
    You can tack on `.RunSynchronously()` if needed. – HappyNomad Mar 10 '13 at 01:44
  • You can also use Microsoft.Bcl.Async's TaskEx.Delay, you cab grab the package using NuGet. – Kanadaj Aug 07 '14 at 22:49
  • If it's awaitable, why didn't they call it `DelayAsync()`? – Jay Carlton Oct 19 '14 at 04:39
  • @Jay: It's felt that methods which are *clearly* about asynchrony are okay without the suffix, e.g. `Task.Run`. – Jon Skeet Oct 19 '14 at 06:26
  • @RAM: Well then it's not a Windows Store app to start with, so we're in a completely different context - I'd suggest that in that case you ask a new question with more details. – Jon Skeet Feb 16 '15 at 16:37
  • @JonSkeet, my platform is **C# .NET 3.5 WPF**. i found some ways but i don't know what is the best and short way. do you know any good answer in stackoverflow for it? I can write a new question but i like know your suggestions. do you suggest new question in stackoverflow? thanks... – Ramin Bateni Feb 16 '15 at 17:21
  • 1
    @RAM: Well `Thread.Sleep` *is* supported in .NET 3.5, so the question doesn't apply. No doubt you have *a* question, but it doesn't sound like it's the same as this one. Please read http://tinyurl.com/stack-hints then ask a new question. – Jon Skeet Feb 16 '15 at 17:22
  • 2
    @RAM: I'm not sure how I was meant to guess that from your comments. There are plenty of similar questions on SO already about WPF and animation. – Jon Skeet Feb 16 '15 at 18:03
46

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

Antony Thomas
  • 3,576
  • 2
  • 34
  • 40
20

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));
}
Johannes Egger
  • 3,874
  • 27
  • 36
11

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!
}
Asherah
  • 18,948
  • 5
  • 53
  • 72
Benny Code
  • 51,456
  • 28
  • 233
  • 198
-7

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 ...

EaterOfCode
  • 2,202
  • 3
  • 20
  • 33
igrimpe
  • 1,775
  • 11
  • 12
  • 9
    I disagree. If the thread is a background thread and the sleep is short then it is more efficient to have it sleep for a few milliseconds than using a timer. – Jason Steele Jan 19 '13 at 19:42
  • 1
    and sometimes you're told to do it despite advising said authority figure of the consequences ;) – Jordan Jan 14 '14 at 21:28