-
Notifications
You must be signed in to change notification settings - Fork 8
/
ThreadSample.cs
53 lines (45 loc) · 1.48 KB
/
ThreadSample.cs
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
using System;
using System.Threading;
namespace CodeSamples.MultiThreading
{
internal class LaborInstructions
{
private readonly AutoResetEvent _syncObject;
public string SomeParameter { get; set; }
public AutoResetEvent SyncObject => _syncObject;
public LaborInstructions(AutoResetEvent syncObject)
{
_syncObject = syncObject;
}
}
internal class ThreadLabor
{
public void DoWork(object instructions)
{
var passedParametersClass = instructions as LaborInstructions;
Console.WriteLine($"Doing labor... {passedParametersClass.SomeParameter} ... and waiting to complete...");
Thread.Sleep(1000);
passedParametersClass.SyncObject.Set();
}
}
public class ThreadSample
{
private readonly AutoResetEvent syncObject = new AutoResetEvent(false);
public void Go()
{
var threadLabor = new ThreadLabor();
var laborInstructions = new LaborInstructions(syncObject)
{
SomeParameter = $"Current DateTime is {DateTime.Now.ToString()}"
};
Console.WriteLine("Going into thread...");
var thread = new Thread(threadLabor.DoWork)
{
Priority = ThreadPriority.Highest
};
thread.Start(laborInstructions);
syncObject.WaitOne();
Console.WriteLine("done!");
}
}
}