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)); }; }
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; }