タスク使用履歴
C〓〓4.0があって、4.0以下のは試しません.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- using System.Threading.Tasks;
- using System.Threading;
-
- namespace TaskDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- int mCount = 0;
-
- CancellationTokenSource tokenSource = new CancellationTokenSource();
- CancellationToken token = tokenSource.Token;
-
- Task t = new Task(() =>
- {
- Thread.Sleep(1000);
- Console.WriteLine(" ……");
- //
- for (int i = 0; i < 3000; i++)
- {
- if (token.IsCancellationRequested == true)
- {
- break; // throw new OperationCanceledException(token);
- }
- mCount++;
- Thread.Sleep(1);
- }
- }, token);
- token.Register(() =>
- {
-
- Console.WriteLine("Canceled");
-
- });
- t.Start();
- Console.WriteLine(" ……");
- t.ContinueWith((task) =>
- {
- Console.WriteLine(" , :");
- Console.WriteLine("IsCanceled={0}\tIsCompleted={1}\tIsFaulted={2}\tmCount={3}", task.IsCanceled, task.IsCompleted, task.IsFaulted, mCount);
- });
- Console.WriteLine(" , ……");
- Console.ReadKey(); // sleep , ,
- tokenSource.Cancel();
- Console.ReadKey(); //
-
- }
- catch (Exception Ex)
- {
- Console.WriteLine(Ex.Message);
- }
-
- }
- }
- }
-
-
-
-