Exemplo n.º 1
0
            public static void Async([NotNull] Func <Task> handler, int boundedCapacity)
            {
                var previousContext = SynchronizationContext.Current;

                try
                {
                    using (var synchronizationContext = new AsyncCmdletSynchronizationContext(boundedCapacity))
                    {
                        SetSynchronizationContext(synchronizationContext);
                        _currentAsyncCmdletContext = synchronizationContext;

                        var task = handler();
                        if (task == null)
                        {
                            return;
                        }

                        var waitable = task.ContinueWith(t => synchronizationContext.Complete(), scheduler: TaskScheduler.Default);

                        synchronizationContext.ProcessQueue();

                        waitable.GetAwaiter().GetResult();
                    }
                }
                finally
                {
                    SetSynchronizationContext(previousContext);
                    _currentAsyncCmdletContext = previousContext as AsyncCmdletSynchronizationContext;
                }
            }
Exemplo n.º 2
0
            public static void Async([NotNull] Func <Task> handler, int boundedCapacity)
            {
                var previousContext = SynchronizationContext.Current;

                try
                {
                    using (var synchronizationContext = new AsyncCmdletSynchronizationContext(boundedCapacity))
                    {
                        SetSynchronizationContext(synchronizationContext);
                        currentAsyncCmdletContext = synchronizationContext;

                        var task = handler();
                        if (task == null)
                        {
                            return;
                        }

                        var waitable = task.ContinueWith(t => synchronizationContext.Complete(), scheduler: TaskScheduler.Default);

                        synchronizationContext.ProcessQueue();

                        waitable.GetAwaiter().GetResult();

                        //error handling - exception is AggregateException therefore throwing inner exception
                        var exception = task.Exception;
                        if (exception != null && exception.InnerException != null)
                        {
                            throw exception.InnerException;
                        }
                    }
                }
                finally
                {
                    SetSynchronizationContext(previousContext);
                    currentAsyncCmdletContext = previousContext as AsyncCmdletSynchronizationContext;
                }
            }