42

I'm porting a library to .NET core and to maximize portability I need to eliminate the dependency on System.Threading.Thread, and therefore Thread.Sleep. Whats an alternative to this?

Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
  • 3
    `await Task.Delay(1000)` – Fabio Jun 26 '17 at 07:15
  • Most of the usual alternatives to `Thread.Sleep()` will work. See marked duplicates for examples. – Peter Duniho Jun 26 '17 at 07:19
  • If a library, one flexible aternative would be a 'delay()' callback so that the user can provide whatever mechanism is available to them. – ThingyWotsit Jun 26 '17 at 08:58
  • 5
    None of the suggested questions are related to .NET Core and do not show up for searches regarding .NET Core so I do not think this is a a duplicate. Especially since the first suggestions is about windows.Forms which does not exist in .NET Core. – David Mårtensson Sep 06 '17 at 12:11

1 Answers1

123
you can use 

Task.Delay(2000).Wait(); // Wait 2 seconds with blocking

await Task.Delay(2000); // Wait 2 seconds without blocking
Vivek Shukla
  • 1,535
  • 1
  • 13
  • 13