TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Sleep Method (Pause)

Use the Sleep method from System.Threading to pause a program. Compare SpinWait with Sleep.
Sleep. This method pauses programs. It receives a value indicating the number of milliseconds to wait. It sometimes helps with diagnosing a problem.Threads
Calling Sleep can be useful for waiting on an external application or task. It does not cause CPU usage during the pause. SpinWait, meanwhile, does cause CPU usage.SpinWait
An example. Sleep is found on the Thread class. It is a static method that receives one parameter. You must either include System.Threading or call System.Threading.Thread.Sleep.

Tip: You do not need to create a new Thread to use the Sleep method as it is static.

Static

Here: We call Sleep() 3 times. The surrounding code takes the system's time and uses Stopwatch to time the Thread.Sleep calls.

Stopwatch
C# program that sleeps using System; using System.Diagnostics; using System.Threading; class Program { static void Main() { // // Demonstrates 3 different ways of calling Sleep. // var stopwatch = Stopwatch.StartNew(); Thread.Sleep(0); stopwatch.Stop(); Console.WriteLine(stopwatch.ElapsedMilliseconds); Console.WriteLine(DateTime.Now.ToLongTimeString()); stopwatch = Stopwatch.StartNew(); Thread.Sleep(5000); stopwatch.Stop(); Console.WriteLine(stopwatch.ElapsedMilliseconds); Console.WriteLine(DateTime.Now.ToLongTimeString()); stopwatch = Stopwatch.StartNew(); System.Threading.Thread.Sleep(1000); stopwatch.Stop(); Console.WriteLine(stopwatch.ElapsedMilliseconds); // // Bonus: shows SpinWait method. // stopwatch = Stopwatch.StartNew(); Thread.SpinWait(100000 * 10000); stopwatch.Stop(); Console.WriteLine(stopwatch.ElapsedMilliseconds); } } Output 0 ElapsedMilliseconds after Sleep(0) 8:14:43 AM Time after Sleep(0) 4999 ElapsedMilliseconds after Sleep(5000) 8:14:48 AM Time after Sleep(5000) 999 ElapsedMilliseconds after Sleep(1000) 3144 ElapsedMilliseconds after SpinWait(Int32)
TimeSpan. We can also use Sleep() with a TimeSpan argument. To sleep for 3 seconds, for example, we can pass in a TimeSpan created with the TimeSpan.FromSeconds method.TimeSpan

Here: The program pauses for 3 seconds when it is executed, then prints a message to the console before it terminates.

C# program that uses TimeSpan overload using System; class Program { static void Main() { // Sleep for 3 seconds. System.Threading.Thread.Sleep(TimeSpan.FromSeconds(3)); Console.WriteLine("[DONE]"); } } Output [DONE]
Notes, usage. Suppose you have a program that modifies external files. You want to see how the files have been modified at a certain point.

So: You can insert a call to Sleep(), and then manually check the files (while Sleep is suspending the program's operation).

Implementation. We review the implementation of Thread.Sleep Thread.SpinLock. These 2 methods call into the SleepInternal and SpinWaitInternal functions in mscorlib.

Tip: Thread.Sleep likely will end up calling the same code in the Windows kernel that the Sleep call in any language uses.

SpinWait. When you execute the first program, you will notice that the program does not require significant CPU time when executing the Thread.Sleep calls.

Important: The program only starts consuming the entire CPU core when the Thread.SpinWait method is invoked.

Info: Sleep uses the operating system to "pause" the program, resulting in 0% CPU, while Thread.SpinWait executes useless instructions.

A summary. Thread.Sleep is fairly accurate in pausing the program for the specified number of milliseconds. It does not cause your program to consume 100% CPU.
When sleep is called, other programs can still use the CPU. This will save power. But usually in simple programs these considerations are not that important.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf