changeset 18:a6faa87767bb

AsyncResultBase: prevent recursive synchronous completion callback
author Ivo Smits <Ivo@UCIS.nl>
date Wed, 10 Apr 2013 01:33:54 +0200
parents 5d9a7186c9f7
children b9ef273964fd
files Util/AsyncResultBase.cs
diffstat 1 files changed, 9 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/Util/AsyncResultBase.cs	Wed Apr 10 01:33:32 2013 +0200
+++ b/Util/AsyncResultBase.cs	Wed Apr 10 01:33:54 2013 +0200
@@ -4,6 +4,8 @@
 
 namespace UCIS.Util {
 	public abstract class AsyncResultBase : IAsyncResult {
+		[ThreadStatic]
+		static Boolean ThreadInCallback = false;
 		ManualResetEvent WaitEvent = null;
 		AsyncCallback Callback = null;
 		public object AsyncState { get; private set; }
@@ -36,8 +38,13 @@
 				if (WaitEvent != null) WaitEvent.Set();
 			}
 			if (Callback != null) {
-				if (synchronously) {
-					Callback(this);
+				if (synchronously && !ThreadInCallback) {
+					try {
+						ThreadInCallback = true;
+						Callback(this);
+					} finally {
+						ThreadInCallback = false;
+					}
 				} else {
 					SysThreadPool.QueueUserWorkItem(CallCallback);
 				}