Esempio n. 1
0
 protected virtual void OnTaskException(TaskExceptionEventArgs args)
 {
     //TODO: (jmd 2015-09-30) Consider wrapping in try catch. They can force the thread to close the app.
     TaskException?.Invoke(this, args);
 }
Esempio n. 2
0
        private async Task Next(CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            if (!_isBusy)
            {
                if (queue.Count > 0)
                {
                    // Optional delay.

                    if (Delay > default(TimeSpan))
                    {
                        await Task.Delay(Delay);
                    }

                    // Peek the current task.

                    var context = queue.Peek();

                    try
                    {
                        _isRunning = true;
                        _isBusy    = true;

                        // Execute the current task.

                        TaskExecuting?.Invoke(this, new TaskEventArgs(context.Tag));

                        await context.Action(context, cts.Token);
                    }
                    catch (Exception exc)
                    {
                        // Handle any exception thrown inside a task.
                        // Invoke the Exception event handlers.

                        TaskException?.Invoke(this, new TaskEventArgs(context.Tag, exc));

                        if (CancelOnException)
                        {
                            // Cancel the queue.

                            ClearInternal();
                        }
                    }

                    TaskExecuted?.Invoke(this, new TaskEventArgs(context.Tag));

                    // Dequeue the currently finished task and request the next.

                    if (queue.Count > 0)
                    {
                        queue.Dequeue();
                    }
                    _isBusy = false;
                    await Next(cts.Token);
                }
            }
            else
            {
                _isRunning = false;
            }
        }