diff 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
line wrap: on
line diff
--- a/Util/AsyncResultBase.cs	Sat Oct 11 14:03:31 2014 +0200
+++ b/Util/AsyncResultBase.cs	Sat Oct 11 14:05:41 2014 +0200
@@ -8,6 +8,7 @@
 		static Boolean ThreadInCallback = false;
 		ManualResetEvent WaitEvent = null;
 		AsyncCallback Callback = null;
+		Object MonitorWaitHandle = new Object();
 		public object AsyncState { get; private set; }
 		public bool CompletedSynchronously { get; private set; }
 		public bool IsCompleted { get; private set; }
@@ -36,6 +37,7 @@
 			lock (this) {
 				IsCompleted = true;
 				if (WaitEvent != null) WaitEvent.Set();
+				if (MonitorWaitHandle != null) lock (MonitorWaitHandle) Monitor.Pulse(MonitorWaitHandle);
 			}
 			if (Callback != null) {
 				if (synchronously && !ThreadInCallback) {
@@ -51,6 +53,10 @@
 			}
 		}
 
+		public void WaitForCompletion() {
+			lock (this) if (!IsCompleted) lock (MonitorWaitHandle) Monitor.Wait(MonitorWaitHandle);
+		}
+
 		protected void ThrowError() {
 			if (Error != null) throw Error;
 		}