using System;
using System.Threading;
using System.Threading.Tasks;
namespace pc.CancellationTokenSource00
{
class Program
{
static void Main(string[] args)
{
int option = -1;
using (var cs = new CancellationTokenSource()) {
var ct = cs.Token;
do{
switch (option){
case 1:
Task.Run(() => {
while (true){
Task.WaitAll(Task.Delay(1000));
Console.WriteLine("thinking");
ct.ThrowIfCancellationRequested();
}
}, ct).ContinueWith(tr=> {
tr.Exception.Handle(e => true);
Console.WriteLine("canceled");
},TaskContinuationOptions.OnlyOnCanceled);
break;
case 2:
cs.Cancel();
break;
}
Console.WriteLine("\n(1)Run (2)Cancel (0)Exit");
} while (int.TryParse(Console.ReadKey().KeyChar.ToString(), out option)
&& option !=
0);
}
}
}
}