TheDeveloperBlog.com

Home | Contact Us

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

<< Back to VBNET

VB.NET Sleep Subroutine (Pause)

Use the Sleep Subroutine from System.Threading. Also invoke the SpinWait Sub.
Sleep. This Subroutine causes a program to suspend operation. It receives 1 parameter—the number of milliseconds to pause the program. No CPU time is used by Sleep. The program simply ceases to operate.Sub
First example. Here we invoke the Sleep Sub on the Thread type. Please note how the Imports System.Threading line is included at the top of the program text. This enables the program to compile correctly.

Calls: Sleep(0) pauses the program for zero milliseconds. Sleep(5000) pauses for five seconds. Sleep(1000) pauses for one second.

SpinWait: We invoke SpinWait. This subroutine results in a lot of CPU usage based on the large integer passed to it.

Tip: SpinWait is the opposite of a Sleep call. It pegs the CPU at 100%.

Stopwatch
VB.NET program that uses the Sleep method Imports System.Threading Module Module1 Sub Main() ' Create a Stopwatch and sleep for zero milliseconds. Dim stopwatch As Stopwatch = stopwatch.StartNew Thread.Sleep(0) stopwatch.Stop() ' Write the current time. Console.WriteLine(stopwatch.ElapsedMilliseconds) Console.WriteLine(DateTime.Now.ToLongTimeString) ' Start a new Stopwatch. stopwatch = stopwatch.StartNew Thread.Sleep(5000) stopwatch.Stop() Console.WriteLine(stopwatch.ElapsedMilliseconds) Console.WriteLine(DateTime.Now.ToLongTimeString) ' Start a new Stopwatch. stopwatch = stopwatch.StartNew Thread.Sleep(1000) stopwatch.Stop() Console.WriteLine(stopwatch.ElapsedMilliseconds) ' Start a new Stopwatch and use SpinWait. stopwatch = stopwatch.StartNew Thread.SpinWait(1000000000) stopwatch.Stop() Console.WriteLine(stopwatch.ElapsedMilliseconds) End Sub End Module Output 0 9:36:02 AM 4999 9:36:07 AM 999 3128
Namespace. To use the Thread.Sleep or Thread.SpinWait methods, please include the Imports System.Threading namespace. These methods are part of Threading, as they are more useful when threads are involved.
A summary. The Thread.Sleep method receives an integer indicating the number of milliseconds you want the program execution to pause. The Thread.SpinWait method receives an integer indicating the amount of work to do. This results in 100% CPU usage.Integer
© 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