1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\r
5 * This software is subject to the Microsoft Public License (Ms-PL).
\r
6 * A copy of the license can be found in the license.htm file included
\r
7 * in this distribution.
\r
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc.Async {
\r
15 using System.Threading;
\r
17 internal static class AsyncUtil {
\r
19 public static void WaitForAsyncResultCompletion(IAsyncResult asyncResult, HttpApplication app) {
\r
20 // based on HttpServerUtility.ExecuteInternal()
\r
22 if (!asyncResult.IsCompleted) {
\r
23 // suspend app lock while waiting, else might deadlock
\r
24 bool needToRelock = false;
\r
27 // .NET 2.0+ will not allow a ThreadAbortException to be thrown while a
\r
28 // thread is inside a finally block, so this pattern ensures that the
\r
29 // value of 'needToRelock' is correct.
\r
33 needToRelock = true;
\r
36 WaitHandle waitHandle = asyncResult.AsyncWaitHandle;
\r
38 if (waitHandle != null) {
\r
39 waitHandle.WaitOne();
\r
42 while (!asyncResult.IsCompleted) {
\r
55 public static AsyncCallback WrapCallbackForSynchronizedExecution(AsyncCallback callback, SynchronizationContext syncContext) {
\r
56 if (callback == null || syncContext == null) {
\r
60 AsyncCallback newCallback = delegate(IAsyncResult asyncResult) {
\r
61 if (asyncResult.CompletedSynchronously) {
\r
62 callback(asyncResult);
\r
65 // Only take the application lock if this request completed asynchronously,
\r
66 // else we might end up in a deadlock situation.
\r
67 syncContext.Sync(() => callback(asyncResult));
\r