-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoResetEvent.linq
36 lines (26 loc) · 917 Bytes
/
AutoResetEvent.linq
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
<Query Kind="Program" />
static AutoResetEvent autoEvent = new AutoResetEvent(false);
static void Main()
{
Console.WriteLine("Main starting.");
ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod), autoEvent);
// Wait for work method to signal.
autoEvent.WaitOne();
Console.WriteLine("Work method 1 signaled.");
//----------------
// b/c using AutoResetEvent we shouldn't have to reset the wait event
//----------------
ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod), autoEvent);
// Wait for work method to signal.
autoEvent.WaitOne();
Console.WriteLine("Work method 2 signaled.\nMain ending.");
}
static void WorkMethod(object stateInfo)
{
Console.WriteLine("Work starting.");
// Simulate time spent working.
Thread.Sleep(TimeSpan.FromSeconds(10));
// Signal that work is finished.
Console.WriteLine("Work ending.");
((AutoResetEvent)stateInfo).Set();
}