Exemplo n.º 1
0
        public void ShouldCancelTasksOnShutdown()
        {
            var executor = CreateDefaultExecutor(ShutdownMode.CancelQueuedTasks);
            var tasks = new List<WaitingTask>();
            var futures = new List<Future<int>>();
            for(int i=0; i<10; i++) {
                WaitingTask task = new WaitingTask();
                tasks.Add(task);
                if(i == 0) {
                    Monitor.Enter(task);
                }
                futures.Add(executor.Submit(task));
            }
            executor.Shutdown();
            Monitor.Exit(tasks[0]);

            while(!executor.IsShutdown()) { }
            Assert.AreEqual(0, futures[0].GetResult());
            foreach(Future<int> future in futures.GetRange(1, 9)) {
                Assert.IsTrue(future.IsDone);
                try {
                    Assert.AreEqual(0, future.GetResult());
                    Assert.Fail(); // Should not reach this
                } catch(ExecutionException e) {
                    Assert.IsInstanceOfType(typeof(TaskCancelledException), e.delayedException);
                }
            }
        }
Exemplo n.º 2
0
        private void FormWaiting_Shown(object sender, EventArgs e)
        {
            WaitingTask.ContinueWith((t) => { this.DialogResult = DialogResult.OK; }
                                     , System.Threading.CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.FromCurrentSynchronizationContext());

            WaitingTask.ContinueWith((t) => { this.DialogResult = DialogResult.Cancel; }
                                     , System.Threading.CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.FromCurrentSynchronizationContext());
            WaitingTask.Start();
        }
Exemplo n.º 3
0
 /// <summary>
 /// <para> 断开连接, 断开后本对象所有资源将被释放 </para>
 /// <seealso cref="TcpClient.Close()"/>
 /// </summary>
 public void Stop(bool nowait = false)
 {
     taskEnd = true;
     Close();
     Dispose();
     if (!nowait)
     {
         WaitingTask.Wait();
     }
 }