示例#1
0
		public static AsyncCallback WrapCallbackForSynchronizedExecution(
			AsyncCallback callback, SynchronizationContext context)
		{
			if (callback == null || context == null)
				return callback;

			return delegate(IAsyncResult result) {
				if (result.CompletedSynchronously)
					callback(result);
				else
					context.Sync(() => callback(result));
			};
		}
示例#2
0
文件: AsyncUtil.cs 项目: nobled/mono
        public static AsyncCallback WrapCallbackForSynchronizedExecution(AsyncCallback callback, SynchronizationContext syncContext) {
            if (callback == null || syncContext == null) {
                return callback;
            }

            AsyncCallback newCallback = delegate(IAsyncResult asyncResult) {
                if (asyncResult.CompletedSynchronously) {
                    callback(asyncResult);
                }
                else {
                    // Only take the application lock if this request completed asynchronously,
                    // else we might end up in a deadlock situation.
                    syncContext.Sync(() => callback(asyncResult));
                }
            };

            return newCallback;
        }