Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ee0bc0d176
Fetching contributors…

Cannot retrieve contributors at this time

file 67 lines (55 sloc) 2.008 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
using System;
using System.Threading;

class EventTest
{
public static AutoResetEvent e;

// Code for first thread
        public static void ThreadMethod_waiter_1()
     {
  Console.WriteLine("[Thread A] - Started.....");
Console.WriteLine("[Thread A] - I'm before wait for event .....");
e.WaitOne();
Console.WriteLine("[Thread A] - I'm after wait for event.");
Console.WriteLine("[Thread A] - I now set again the event to let other thread continue.");
e.Set();
}
    
// Code for second thread
        public static void ThreadMethod_waiter_2()
     {
  Console.WriteLine("[Thread B] - Started.....");
Console.WriteLine("[Thread B] - I'm before wait for event .....");
e.WaitOne();
Console.WriteLine("[Thread B] - I'm after wait for event.");
Console.WriteLine("[Thread B] - I now set again the event to let other thread continue.");
     e.Set();
}

    // Code for 3th thread
    public static void ThreadMethod_blocker()
    {
  Console.WriteLine("[Thread C] - Started.....");
Console.WriteLine("[Thread C] - Sleeping for 5000ms....");
Thread.Sleep(5000);
Console.WriteLine("[Thread C] - Setting the event....");
e.Set();
Console.WriteLine("[Thread C] - Finished.....");
    }
    
    
    public static void Main()
    {
     e = new AutoResetEvent(false);

   
        // Create the waiter thread's group
        Console.WriteLine("[ Main ] - Creating first thread..");
        ThreadStart Thread_1 = new ThreadStart(ThreadMethod_waiter_1);
        ThreadStart Thread_2 = new ThreadStart(ThreadMethod_waiter_2);
        
        // Create the blocker thread
        Console.WriteLine("[ Main ] - Creating second thread..");
        ThreadStart Thread_3 = new ThreadStart(ThreadMethod_blocker);

        Thread A = new Thread(Thread_1);
        Thread B = new Thread(Thread_2);
        Thread C = new Thread(Thread_3);
        
A.Start();
     B.Start();
     C.Start();
    
     Thread.Sleep(500);
     Console.WriteLine("[ Main ] - Finish...");
    }
}
Something went wrong with that request. Please try again.