HI WELCOME TO Sirees

C# Threading Example: Sleep() method

Leave a Comment
The Sleep() method suspends the current thread for the specified milliseconds. So, other threads get the chance to start execution.
  1. using System;  
  2. using System.Threading;  
  3. public class MyThread  
  4. {  
  5.     public void Thread1()  
  6.     {  
  7.         for (int i = 0; i < 10; i++)  
  8.         {  
  9.             Console.WriteLine(i);  
  10.             Thread.Sleep(200);  
  11.         }  
  12.     }  
  13. }  
  14. public class ThreadExample  
  15. {  
  16.     public static void Main()  
  17.     {  
  18.         MyThread mt = new MyThread();  
  19.         Thread t1 = new Thread(new ThreadStart(mt.Thread1));  
  20.         Thread t2 = new Thread(new ThreadStart(mt.Thread1));  
  21.         t1.Start();  
  22.         t2.Start();  
  23.     }  
  24. }  
Output:
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.