Quantcast
Channel: CodesMesh » Syed Wajahat Ali Shah
Viewing all articles
Browse latest Browse all 11

C# sleep function to give delay

$
0
0

In C# delay is given using Thread.Sleep(t) function with ‘t’ is representing time for which we want delay in milliseconds. For one second delay we write following statement.


For two seconds delay give it ’2000′ as argument. Basically this function puts delay in your program by stopping the main program thread for the time mentioned.

Delay can also be given using a loop. A for loop or a while loop may be used for this purpose. It has an empty statement but it will iterate thousand of times which will take some time and will give us a delay.

Delay using for loop may be given as shown below


But the preferred method is by using Thread.Sleep() method. Following code gives us an example of using this method.

Example:

Remember for using threads you need to include this line at top of your program for using threads and their features.


This is an example of delay in C# using both ways i.e Thread.Sleep() and loops.


Viewing all articles
Browse latest Browse all 11

Trending Articles