コード例 #1
0
        internal static void OnCompletedInternal(Task task, Action continuation, bool continueOnCapturedContext)
        {
            if (continuation == null)
            {
                throw new ArgumentNullException("continuation");
            }
            SynchronizationContext sc = continueOnCapturedContext ? SynchronizationContext.Current : null;

            if (sc != null && sc.GetType() != typeof(SynchronizationContext))
            {
                task.ContinueWith(delegate(Task param0)
                {
                    try
                    {
                        sc.Post(delegate(object state)
                        {
                            ((Action)state)();
                        }, continuation);
                    }
                    catch (Exception exception)
                    {
                        AsyncMethodBuilderCore.ThrowAsync(exception, null);
                    }
                }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
                return;
            }
            TaskScheduler taskScheduler = continueOnCapturedContext ? TaskScheduler.Current : TaskScheduler.Default;

            if (task.IsCompleted)
            {
                Task.Factory.StartNew(delegate(object s)
                {
                    ((Action)s)();
                }, continuation, CancellationToken.None, TaskCreationOptions.None, taskScheduler);
                return;
            }
            if (taskScheduler != TaskScheduler.Default)
            {
                task.ContinueWith(delegate(Task _)
                {
                    TaskAwaiter.RunNoException(continuation);
                }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, taskScheduler);
                return;
            }
            task.ContinueWith(delegate(Task param0)
            {
                if (TaskAwaiter.IsValidLocationForInlining)
                {
                    TaskAwaiter.RunNoException(continuation);
                    return;
                }
                Task.Factory.StartNew(delegate(object s)
                {
                    TaskAwaiter.RunNoException((Action)s);
                }, continuation, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
            }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
        }