示例#1
0
        /// <summary>
        /// Schedules action to be executed.
        /// </summary>
        public IDisposable Schedule(Action action)
        {
            if (action == null)
                throw new ArgumentNullException("action");

            var cancelable = new CancellationDisposable();
            taskFactory.StartNew(() =>
                {
                    try
                    {
                        action();
                    }
                    catch (Exception ex)
                    {
                        var raise = new Thread(() => { throw ex.PrepareForRethrow(); });
                        raise.Start();
                        raise.Join();
                    }
                }, cancelable.Token);
            return cancelable;
        }
示例#2
0
 public void CancellationDisposable_TokenCtor()
 {
     var t = new CancellationTokenSource();
     var c = new CancellationDisposable(t);
     Assert.IsTrue(t.Token == c.Token);
     Assert.IsFalse(c.Token.IsCancellationRequested);
     Assert.IsTrue(c.Token.CanBeCanceled);
     c.Dispose();
     Assert.IsTrue(c.Token.IsCancellationRequested);
 }
示例#3
0
 public void CancellationDisposable_DefaultCtor()
 {
     var c = new CancellationDisposable();
     Assert.IsNotNull(c.Token);
     Assert.IsFalse(c.Token.IsCancellationRequested);
     Assert.IsTrue(c.Token.CanBeCanceled);
     c.Dispose();
     Assert.IsTrue(c.Token.IsCancellationRequested);
 }