Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AsyncUtil.cs « Async « System.Web.Mvc « src - github.com/mono/aspnetwebstack.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4c75be0b12499dc7be0e5782e275b7309ea68f27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System.Threading;

namespace System.Web.Mvc.Async
{
    internal static class AsyncUtil
    {
        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;
        }
    }
}