Advertisement
mvassilev

Untitled

Feb 25th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. public class ThreadExample {
  5.     public static void ThreadProc() {
  6.         for (int i = 0; i < 20; i++) {
  7.             Console.WriteLine("ThreadProc: {0}", i);
  8.             Thread.Sleep(0);
  9.         }
  10.     }
  11.  
  12.     public static void Main() {
  13.         Console.WriteLine("Main thread: Start a second thread.");
  14.         Thread t1 = new Thread(new ThreadStart(ThreadProc));
  15.  
  16.         t1.Start();
  17.  
  18.         for (int i = 0; i < 20; i++) {
  19.             Console.WriteLine("Main thread: Do some work.");
  20.             Thread.Sleep(0);
  21.         }
  22.  
  23.         Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
  24.  
  25.         t1.Join();
  26.         Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
  27.         Console.ReadLine();
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement