Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread.Sleep alternative in .NET Core [duplicate]

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?

like image 677
Robin Rodricks Avatar asked Jun 26 '17 07:06

Robin Rodricks


People also ask

What is the alternative for thread sleep in Java?

TimeUnit provides a human-readable version of the Thread. sleep() method which can be used in place of the former. For a long time Thread's sleep() method is a standard way to pause a Thread in Java and almost every Java programmer is familiar with that.

How long is thread sleep 1000?

For example, with thread. sleep(1000), you intended 1,000 milliseconds, but it could potentially sleep for more than 1,000 milliseconds too as it waits for its turn in the scheduler. Each thread has its own use of CPU and virtual memory.

Is Task delay better than thread sleep?

delay will not and has a cancellation token, unless your app is pretty complex, it really doesn't matter as on the surface: task. delay and thread. sleep do pretty much the same thing. "both do the same thing, they block the executing thread..." -- AFAIK the await task.

What's thread sleep () in threading in C#?

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. There are two methods in the overload list of Thread. Sleep Method as follows: Sleep(Int32)


1 Answers

you can use   Task.Delay(2000).Wait(); // Wait 2 seconds with blocking  await Task.Delay(2000); // Wait 2 seconds without blocking 
like image 121
Vivek Shukla Avatar answered Sep 23 '22 16:09

Vivek Shukla