public void Dispose()
        {
            bool   lockTaken = false;
            object obj       = null;
            Task   task;

            try
            {
                Monitor.Enter(obj = this._lock, ref lockTaken);
                if (this._isDisposed)
                {
                    return;
                }
                this._isDisposed = true;
                task             = this._task;
                this._handler    = (Func <Task>)null;
            }
            finally
            {
                if (lockTaken)
                {
                    Monitor.Exit(obj);
                }
            }
            if (!this._cancellationTokenSource.IsCancellationRequested)
            {
                this._cancellationTokenSource.Cancel();
            }
            if (null == task)
            {
                return;
            }
            try
            {
                task.Wait();
            }
            catch (OperationCanceledException ex)
            {
            }
            catch (AggregateException ex)
            {
                if (Enumerable.Any <Exception>((IEnumerable <Exception>)ex.Flatten().InnerExceptions, (Func <Exception, bool>)(e => !(e is OperationCanceledException))))
                {
                    Debug.WriteLine("SignalTask.Dispose(): " + ExceptionExtensions.ExtendedMessage((Exception)ex));
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("SignalTask.Dispose(): " + ExceptionExtensions.ExtendedMessage(ex));
            }
            this._cancellationTokenSource.Dispose();
        }
Exemple #2
0
 private async Task Worker()
 {
     while (true)
     {
         bool lockTaken = false;
         AsyncFifoWorker.WorkHandle work;
         object obj = null;
         try
         {
             Monitor.Enter(obj = this._lock, ref lockTaken);
             if (this._workQueue.Count >= 1)
             {
                 work = this._workQueue.Dequeue();
             }
             else
             {
                 break;
             }
         }
         finally
         {
             if (lockTaken)
             {
                 Monitor.Exit(obj);
             }
         }
         this._work = work;
         try
         {
             work.TryDeregister();
             await work.RunAsync().ConfigureAwait(false);
         }
         catch (Exception ex)
         {
             Debug.WriteLine("AsyncFifoWorker.Worker() failed: " + ExceptionExtensions.ExtendedMessage(ex));
         }
         this._work = (AsyncFifoWorker.WorkHandle)null;
         work.Dispose();
     }
 }
Exemple #3
0
 private static void ReportException(Task task, string description)
 {
     try
     {
         AggregateException exception = task.Exception;
         if (null == exception)
         {
             return;
         }
         Debug.WriteLine("TaskCollector.Cleanup() task {0} failed: {1}{2}{3}", (object)description, (object)ExceptionExtensions.ExtendedMessage((Exception)exception), (object)Environment.NewLine, (object)exception.StackTrace);
         if (Debugger.IsAttached)
         {
             Debugger.Break();
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("TaskCollector.Cleanup() cleanup of task {0} failed: {1}", (object)description, (object)ExceptionExtensions.ExtendedMessage(ex));
         if (!Debugger.IsAttached)
         {
             return;
         }
         Debugger.Break();
     }
 }
Exemple #4
0
            public async Task RunAsync()
            {
                TaskCompletionSource <object> tcs = this._taskCompletionSource;

                try
                {
                    await this._work().ConfigureAwait(false);

                    if (null != tcs)
                    {
                        tcs.TrySetResult((object)string.Empty);
                    }
                }
                catch (OperationCanceledException ex)
                {
                    if (null != tcs)
                    {
                        tcs.TrySetCanceled();
                    }
                }
                catch (Exception ex)
                {
                    if (tcs == null || !tcs.TrySetException(ex))
                    {
                        Debug.WriteLine("AsyncFifoWorker.WorkHandle.RunAsync() work should not throw exceptions: " + ExceptionExtensions.ExtendedMessage(ex));
                    }
                    else
                    {
                        goto label_9;
                    }
                }
                label_9 :;
            }