comparison Util/AsyncResultBase.cs @ 104:327be9216006

Improved PML code
author Ivo Smits <Ivo@UCIS.nl>
date Sat, 11 Oct 2014 14:05:41 +0200
parents a6faa87767bb
children
comparison
equal deleted inserted replaced
103:8fe322656807 104:327be9216006
6 public abstract class AsyncResultBase : IAsyncResult { 6 public abstract class AsyncResultBase : IAsyncResult {
7 [ThreadStatic] 7 [ThreadStatic]
8 static Boolean ThreadInCallback = false; 8 static Boolean ThreadInCallback = false;
9 ManualResetEvent WaitEvent = null; 9 ManualResetEvent WaitEvent = null;
10 AsyncCallback Callback = null; 10 AsyncCallback Callback = null;
11 Object MonitorWaitHandle = new Object();
11 public object AsyncState { get; private set; } 12 public object AsyncState { get; private set; }
12 public bool CompletedSynchronously { get; private set; } 13 public bool CompletedSynchronously { get; private set; }
13 public bool IsCompleted { get; private set; } 14 public bool IsCompleted { get; private set; }
14 public Exception Error { get; private set; } 15 public Exception Error { get; private set; }
15 public WaitHandle AsyncWaitHandle { 16 public WaitHandle AsyncWaitHandle {
34 this.CompletedSynchronously = synchronously; 35 this.CompletedSynchronously = synchronously;
35 this.Error = error; 36 this.Error = error;
36 lock (this) { 37 lock (this) {
37 IsCompleted = true; 38 IsCompleted = true;
38 if (WaitEvent != null) WaitEvent.Set(); 39 if (WaitEvent != null) WaitEvent.Set();
40 if (MonitorWaitHandle != null) lock (MonitorWaitHandle) Monitor.Pulse(MonitorWaitHandle);
39 } 41 }
40 if (Callback != null) { 42 if (Callback != null) {
41 if (synchronously && !ThreadInCallback) { 43 if (synchronously && !ThreadInCallback) {
42 try { 44 try {
43 ThreadInCallback = true; 45 ThreadInCallback = true;
49 SysThreadPool.QueueUserWorkItem(CallCallback); 51 SysThreadPool.QueueUserWorkItem(CallCallback);
50 } 52 }
51 } 53 }
52 } 54 }
53 55
56 public void WaitForCompletion() {
57 lock (this) if (!IsCompleted) lock (MonitorWaitHandle) Monitor.Wait(MonitorWaitHandle);
58 }
59
54 protected void ThrowError() { 60 protected void ThrowError() {
55 if (Error != null) throw Error; 61 if (Error != null) throw Error;
56 } 62 }
57 } 63 }
58 } 64 }