LoginSignup
3
4

More than 5 years have passed since last update.

継続タスクでのキャンセル処理

Last updated at Posted at 2014-11-29

キャンセルするとキャンセル発生元のタスクは(何もしなければ)最後まで処理が行われ、それ以降の継続タスクは処理されない。

また、OperationCanceledExceptionがスロー・AggregateExceptionに集約される。キャンセル可能性があるタスクは呼び出しもとで補足・後処理すべし。

検証用コード

コンソールアプリケーションで動かしました。

public static class ContinueWithExample3
{
    public static void Run()
    {
        var csource = new CancellationTokenSource();

        var task = Task.Factory.StartNew(() =>
        {
            Console.Write("入力:");
            var input = Console.ReadLine();

            if (input.ToLower() == "cancel")
            {
                csource.Cancel();
            }

            if (input.ToLower() == "error")
            {
                throw new Exception("何らかのエラー");
            }

            if (input.ToLower() == "error2")
            {
                throw new Exception("何らかのエラー2");
            }

        })
        .ContinueWith((t) =>
        {
            if (t.IsFaulted && t.Exception.Flatten().InnerExceptions.Any((e) => e.Message == "何らかのエラー2"))
            {
                Console.WriteLine("Task1でタスクの取り消しを行います。");
                csource.Cancel();
            }

            Console.WriteLine("Task1 Cancel:{0}", t.IsCanceled);
            Console.WriteLine("Task1 Faulted:{0}", t.IsFaulted);

        }, csource.Token)

        .ContinueWith((t) =>
        {
            Console.WriteLine("Task2 Cancel:{0}", t.IsCanceled);
            Console.WriteLine("Task2 Faulted:{0}", t.IsFaulted);
        }, csource.Token)

        .ContinueWith((t) =>
        {
            Console.WriteLine("Task3 Cancel:{0}", t.IsCanceled);
            Console.WriteLine("Task3 Faulted:{0}", t.IsFaulted);
        }, csource.Token);

        try
        {
            task.Wait();
        }
        catch (AggregateException exc)
        {
            foreach (var innnerExc in exc.InnerExceptions)
            {
                Console.WriteLine("エラー:{0} {1}",innnerExc.Message,innnerExc.GetType().FullName);
            }
        }
    }
}

実行結果

「cancel」入力

cancel.png

「error」入力

error.png

「error2」入力

error2.png

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4