Advertisement
MagnusArias

PS1 | Asynchronous 1

Apr 25th, 2017
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. public class Program
  5. {
  6.     static Thread thread1, thread2;
  7.  
  8.     public static void Main()
  9.     {
  10.         thread1 = new Thread(ThreadProc);
  11.         thread1.Name = "ThreadMain";
  12.         thread1.Start();
  13.  
  14.         thread2 = new Thread(ThreadProcHello);
  15.         thread2.Name = "ThreadHello";
  16.         thread2.Start();
  17.  
  18.         Console.ReadLine();
  19.     }
  20.  
  21.     private static void ThreadProc()
  22.     {
  23.         if (Thread.CurrentThread.Name == "ThreadMain" && thread2.ThreadState != ThreadState.Unstarted)
  24.         {
  25.             Console.WriteLine("Thread: {0} joined", thread1.Name);
  26.             thread2.Join();
  27.            
  28.         }
  29.  
  30.        
  31.        
  32.         Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
  33.         Console.WriteLine("Thread1: {0}", thread1.ThreadState);
  34.         Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);
  35.         int i = 0;
  36.         while (i++ < 3)
  37.         {
  38.             Thread.Sleep(1000);
  39.             Console.Out.Write("> ");
  40.         }
  41.     }
  42.  
  43.     private static void ThreadProcHello()
  44.     {
  45.         Console.WriteLine("HELLO WORLD!");
  46.      
  47.         Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
  48.         Console.WriteLine("Thread1: {0}", thread1.ThreadState);
  49.         Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);
  50.         int k = 0;
  51.         while (k++ < 3)
  52.         {
  53.             Thread.Sleep(1000);
  54.             Console.Out.Write("> ");
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement