public class EventTester IDisposable private readonly Action _postHocT

 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
public class EventTester: IDisposable
{
private readonly Action _postHocTests;
private readonly ManualResetEvent _waitHandle;
private bool _called;
private IAsyncResult _waitToken;
public EventTester(Action postHocTests)
{
_waitHandle = new ManualResetEvent(false);
_postHocTests = postHocTests;
}
#region IDisposable Members
public void Dispose()
{
lock (_waitHandle)
{
Assert.IsTrue(_called, «The event was never handled»);
_postHocTests.EndInvoke(_waitToken);
}
}
#endregion
public void Handler(TSender sender, TArgument args)
{
lock (_waitHandle)
{
_waitHandle.Set();
_waitToken = _postHocTests.BeginInvoke(sender, args, null, null);
}
}
public void Wait(int mullisecondsTimeout)
{
_called = _waitHandle.WaitOne(mullisecondsTimeout);
}
}