Example #1
0
        public bool TryComplete(T result)
        {
            var completed = false;

            if (Interlocked.Exchange(ref _isCompleted, 1) == 0)
            {
                TaskRunner.RunInBackground(() => _completion.SetResult(result));
                completed = true;
            }
            else
            {
                completed = false;
            }
            Task.Wait();
            return(completed);
        }
Example #2
0
 protected void RunWithTimeout(TimeSpan timeout, Func <Task> func)
 {
     using (var cancellation = new CancellationTokenSource(timeout))
     {
         try
         {
             TaskRunner
             .RunInBackground(func, cancellation.Token)
             .WithCancellation(cancellation.Token)
             .GetResult();
         }
         catch (OperationCanceledException) when(cancellation.IsCancellationRequested)
         {
             throw new TimeoutException($"Task not completed after {timeout.TotalMilliseconds} ms");
         }
     }
 }
Example #3
0
        public bool TryCancel()
        {
            var completed = false;

            if (Interlocked.Exchange(ref _isCompleted, 1) == 0)
            {
                TaskRunner.RunInBackground(() => _completion.SetCanceled());
                completed = true;
            }
            else
            {
                completed = false;
            }
            try
            {
                Task.Wait();
            }
            catch
            {
                // ignored
            }
            return(completed);
        }
Example #4
0
        public bool TryFail(IEnumerable <Exception> errors)
        {
            var completed = false;

            if (Interlocked.Exchange(ref _isCompleted, 1) == 0)
            {
                TaskRunner.RunInBackground(() => _completion.SetException(errors));
                completed = true;
            }
            else
            {
                completed = false;
            }
            try
            {
                Task.Wait();
            }
            catch
            {
                // ignored
            }
            return(completed);
        }
Example #5
0
 public void Dispose()
 {
     _log.Trace("Disposing");
     TaskRunner.RunInBackground(StopAsync).LogAndIgnoreExceptions(_log).GetResult();
 }
Example #6
0
 protected void RunWithTimeout(TimeSpan timeout, Func <Task> func) =>
 Should.CompleteIn(() => TaskRunner.RunInBackground(func), timeout);