Esempio n. 1
0
        /// <summary>
        /// Clear the queue.
        /// </summary>
        public void Clear()
        {
            if (_isBusy)
            {
                throw new InvalidOperationException();
            }

            while (queue.Count > 0)
            {
                var context = queue.Dequeue();
                TaskCanceled?.Invoke(this, new TaskEventArgs(context.Tag));
            }
        }
Esempio n. 2
0
        private void TerminateTask(RunningTask runningTask)
        {
            Log.Debug($"Terminating task {runningTask}");

            lock (_lock)
            {
                for (var i = 0; i < _runningTasks.Count; i++)
                {
                    if (ReferenceEquals(_runningTasks[i].RunningTask, runningTask))
                    {
                        _cancelledTokenSources.Add(runningTask.CancellationTokenSource.Token);
                        runningTask.CancellationTokenSource.Cancel();

                        TaskCanceled?.Invoke(this, new TaskEventArgs(runningTask));
                        return;
                    }
                }
            }
        }
Esempio n. 3
0
        public async Task <ResponseInfo> SendAsync(
            Func <HttpRequestMessage> generator,
            int maximalRetryAttempt = 5)
        {
            var policy = Policy
                         .Handle <HttpRequestException>()
                         .OrResult <ResponseInfo>(response =>
                                                  response.Status is >= HttpStatusCode.InternalServerError or HttpStatusCode.RequestTimeout)
                         .WaitAndRetryAsync(
                maximalRetryAttempt,
                attempt => TimeSpan.FromSeconds(attempt),
                (result, span, attempt, _) => ReceiveFailed?.Invoke(result, span, attempt));

            ResponseInfo info;

            try
            {
                info = await policy.ExecuteAsync(async() =>
                {
                    using var msg = generator();
                    using var res = await Client.SendAsync(msg);

                    return(await res.Content
                           .Decompress()
                           .ContinueWith(t => new ResponseInfo(t.Result, res.StatusCode)));
                });
            }
            catch (TaskCanceledException)
            {
                TaskCanceled?.Invoke();
                return(null);
            }
            catch (Exception e)
            {
                ExceptionThrown?.Invoke(e);
                return(null);
            }

            return(info);
        }
Esempio n. 4
0
 private void OnTaskCanceled(ITaskObserver task, object sender = null)
 {
     TaskCanceled?.Invoke(sender ?? this, new TaskModifiedEventArgs(task, TaskObserveStatus.Canceled));
 }
 private void OnTaskCanceled()
 {
     TaskCanceled?.Invoke(this, new ObservableTaskEventArgs(_task));
 }