Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to Thread.Sleep in C#?

Tags:

People also ask

What can we use instead of thread sleep?

You could also do throws InterruptedException in method definition to avoid try/catch block , or use TimeUnit. MILLISECONDS. sleep() instead.

Why thread sleep is not recommended?

Thread. sleep is bad! It blocks the current thread and renders it unusable for further work.

Is thread sleep a good practice?

Using Thread. sleep() frequently in an automation framework is not a good practice. If the applied sleep is of 5 secs and the web element is displayed in 2 secs only, the extra 3 secs will increase the execution time. And if you use it more often in the framework, the execution time would increase drastically.

What can I use instead of sleep in C++?

C++ language does not provide a sleep function of its own. However, the operating system's specific files like unistd. h for Linux and Windows. h for Windows provide this functionality.


I have a code which when run, it executes series of lines in sequence. I would like to add a pause in between.

Currently, I have it like this

//do work
Thread.Sleep(10800000);
//do work

This however freezes the software, and I think it's because sleep time is way too long. I searched online and I found another work around called Timer. Can someone show me a sample code for Timer which works just like Thread.sleep?

Thank you in advance.

Edit : I need the software to wait for 3 hours before proceeding with rest of the code execution. This software is meant to communicate with machines, and I cannot let the rest of the code execute while waiting for 3 hours.