Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press
Buy directly from Paradoxal Press at $33.99 (Save 43%)
Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA
|
Listing 5-14 extracted from chapter
Processes, threads and synchronization
Listing 5-13< > Listing 5-15
This listing can be compiled with the command line: csc.exe /target:exe Example_5_14.cs Errors: 0 Warnings: 0
Example_5_14.cs
using System;
using System.Threading;
class Program {
static EventWaitHandle[] events;
static void Main() {
events = new EventWaitHandle[2];
// Initial event state: false
events[0] = new EventWaitHandle( false ,EventResetMode.AutoReset );
events[1] = new EventWaitHandle( false ,EventResetMode.AutoReset );
Thread t0 = new Thread( ThreadProc0 );
Thread t1 = new Thread( ThreadProc1 );
t0.Start(); t1.Start();
AutoResetEvent.WaitAll( events );
Console.WriteLine( "MainThread: Thread0 reached 2" +
" and Thread1 reached 3." );
t0.Join();t1.Join();
}
static void ThreadProc0() {
for ( int i = 0; i < 5; i++ ) {
Console.WriteLine( "Thread0: {0}", i );
if ( i == 2 ) events[0].Set();
Thread.Sleep( 100 );
}
}
static void ThreadProc1() {
for ( int i = 0; i < 5; i++ ) {
Console.WriteLine( "Thread1: {0}", i );
if ( i == 3 ) events[1].Set();
Thread.Sleep( 60 );
}
}
}
Copyright Patrick Smacchia 2006 2007
|