Use a manual event object. : ManualResetEvent « Thread « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Thread » ManualResetEvent 
20.25.1.Use a manual event object.
using System;   
using System.Threading;   
  
class MyThread {   
  public Thread thrd;  
  ManualResetEvent manualResetEvent;  
   
  public MyThread(string name, ManualResetEvent e) {   
    thrd = new Thread(this.run);   
    thrd.Name = name;  
    manualResetEvent = e;  
    thrd.Start();   
  }   
   
  void run() {   
    Console.WriteLine("Inside thread " + thrd.Name);  
  
    for(int i=0; i<50; i++) {  
      Console.WriteLine(thrd.Name);  
      Thread.Sleep(50);  
    }  
  
    Console.WriteLine(thrd.Name + " Done!");  
  
    manualResetEvent.Set();
  }   
}   
   
class MainClass {   
  public static void Main() {   
    ManualResetEvent evtObj = new ManualResetEvent(false);  
  
    MyThread myThread = new MyThread("Event Thread 1", evtObj);   

    Console.WriteLine("Main thread waiting for event.");

    // Wait for signaled event.
    evtObj.WaitOne();

    Console.WriteLine("Main thread received first event.");

    evtObj.Reset()

    myThread = new MyThread("Event Thread 2", evtObj);   

    // Wait for signaled event.
    evtObj.WaitOne();

    Console.WriteLine("Main thread received second event.");

  }   
}
Inside thread Event Thread 1
Event Thread 1
Main thread waiting for event.
Event Thread 1
Event Thread 1
Event Thread 1
Event Thread 1
Event Thread 1
Event Thread 1
Event Thread 1
Event Thread 1
^CTerminate batch job (Y/N)? n
20.25.ManualResetEvent
20.25.1.Use a manual event object.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.