다음을 통해 공유


방법: 작업 및 해당 자식 취소

이 예제에서는 다음과 같은 작업을 수행하는 방법을 보여 줍니다.

  1. 취소할 수 있는 작업을 만들고 시작합니다.

  2. 사용자 대리자 및 작업 인스턴스(선택적)에 취소 토큰을 전달합니다.

  3. 사용자 대리자에서 취소 요청을 인식하고 이에 응답합니다.

  4. 필요한 경우 호출 스레드에 작업이 취소되었음을 알립니다.

호출 스레드에서는 작업을 강제로 종료하는 것이 아니라 취소가 요청되었음을 알리기만 합니다. 작업이 이미 실행 중인 경우에는 사용자 대리자에서 해당 요청을 인식하고 적절하게 응답합니다. 작업이 실행되기 전에 취소가 요청된 경우에는 사용자 대리자가 실행되지 않으며 작업 개체는 Canceled 상태로 전환됩니다.

예제

이 예제에서는 취소 요청에 대한 응답으로 Task와 해당 자식 작업을 종료하는 방법을 보여 줍니다. 또한 OperationCanceledException을 throw하여 사용자 대리자가 종료될 때 호출 스레드에서 필요에 따라 Wait 메서드나 WaitAll 메서드를 사용하여 작업이 종료될 때까지 대기할 수 있음을 보여 줍니다. 이 경우 대리자는 try catch 블록을 사용하여 호출 스레드에서 예외를 처리해야 합니다.

' How to: Cancel a Task and Its Children
Imports System.Threading
Imports System.Threading.Tasks

Module CancelATask

    Sub Main()

        Console.WriteLine("Press any key to start. Press 'c' to cancel.")
        Console.ReadKey()

        Dim tokenSource As New CancellationTokenSource()
        Dim token As CancellationToken = tokenSource.Token

        ' Store references to the tasks so that we can wait on them and
        ' observe their status after cancellation.
        Dim tasks(10) As Task

        ' Request cancellation of a single task when the token source is canceled.
        ' Pass the token to the user delegate, and also to the task so it can 
        ' handle the exception correctly.
        tasks(0) = Task.Factory.StartNew(Sub() DoSomeWork(1, token), token)

        ' Request cancellation of a task and its children. Note the token is passed
        ' to (1) the user delegate and (2) as the second argument to StartNew, so 
        ' that the task instance can correctly handle the OperationCanceledException.
        tasks(1) = Task.Factory.StartNew(Sub()

                                             ' Create some cancelable child tasks.
                                             For i As Integer = 2 To 10

                                                 ' For each child task, pass the same token
                                                 ' to each user delegate and to StartNew.
                                                 tasks(i) = Task.Factory.StartNew(Sub(iteration) DoSomeWork(iteration, token), i, token)
                                                 ' Passing the same token again to do work on the parent task. 
                                                 ' All will be signaled by the call to tokenSource.Cancel below.
                                                 DoSomeWork(2, token)
                                             Next
                                         End Sub _
                                , token)

        ' Give the tasks a second to start.
        Thread.Sleep(1000)

        ' Request cancellation from the UI thread.
        If Console.ReadKey().KeyChar = "c"c Then

            tokenSource.Cancel()
            Console.WriteLine("\nTask cancellation requested.")

            ' Optional: Observe the change in the Status property on the task.
            ' It is not necessary to wait on tasks that have canceled. However,
            ' if you do wait, you must enclose the call in a try-catch block to
            ' catch the OperationCanceledExceptions that are thrown. If you do 
            ' not wait, no OCE is thrown if the token that was passed to the 
            ' StartNew method is the same token that requested the cancellation.
        End If


        Try

            Task.WaitAll(tasks)

        Catch e As AggregateException

            ' For demonstration purposes, show the OCE message.
            For Each v In e.InnerExceptions
                Console.WriteLine("msg: " + v.Message)
            Next
        End Try

        ' Prove that the tasks are now all in a canceled state.
        For i As Integer = 0 To tasks.Length
            Console.WriteLine("task(0) status is now 1", i, tasks(i).Status)
        Next

        ' Keep the console window open while the
        ' task completes its output.
        Console.ReadLine()
    End Sub

    Sub DoSomeWork(ByVal taskNum As Integer, ByVal ct As CancellationToken)

        ' Was cancellation already requested?
        If ct.IsCancellationRequested = True Then
            Console.WriteLine("We were cancelled before we got started.")
            Console.WriteLine("Press Enter to quit.")
            ct.ThrowIfCancellationRequested()
        End If

        Dim maxIterations As Integer = 1000

        ' NOTE!!! An "OperationCanceledException was unhandled
        ' by user code" error will be raised here if "Just My Code"
        ' is enabled on your computer. On Express editions JMC is
        ' enabled and cannot be disabled. The exception is benign.
        ' Just press F5 to continue executing your code.
        For i As Integer = 0 To maxIterations
            ' Do a bit of work. Not too much.
            Dim sw As New SpinWait()
            For j As Integer = 0 To 3000
                sw.SpinOnce()
            Next
            Console.WriteLine("...0 ", taskNum)
            If ct.IsCancellationRequested Then

                Console.WriteLine("bye from 0.", taskNum)
                Console.WriteLine("\nPress Enter to quit.")

                ct.ThrowIfCancellationRequested()
            End If
        Next

    End Sub

End Module

namespace CancellationWithOCE
{
    using System;
    using System.Threading;
    using System.Threading.Tasks;

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press any key to start. Press 'c' to cancel.");
            Console.ReadKey();

            var tokenSource = new CancellationTokenSource();
            var token = tokenSource.Token;

            // Store references to the tasks so that we can wait on them and
            // observe their status after cancellation.
            Task[] tasks = new Task[10];

            // Request cancellation of a single task when the token source is canceled.
            // Pass the token to the user delegate, and also to the task so it can 
            // handle the exception correctly.
            tasks[0] = Task.Factory.StartNew(() => DoSomeWork(1, token), token);

            // Request cancellation of a task and its children. Note the token is passed
            // to (1) the user delegate and (2) as the second argument to StartNew, so 
            // that the task instance can correctly handle the OperationCanceledException.
            tasks[1] = Task.Factory.StartNew(() =>
            {
                // Create some cancelable child tasks.
                for (int i = 2; i < 10; i++)
                {
                    // For each child task, pass the same token
                    // to each user delegate and to StartNew.
                    tasks[i] = Task.Factory.StartNew(iteration =>
                                DoSomeWork((int)iteration, token), i, token);
                }
                // Passing the same token again to do work on the parent task. 
                // All will be signaled by the call to tokenSource.Cancel below.
                DoSomeWork(2, token);
            }, token);

            // Give the tasks a second to start.
            Thread.Sleep(1000);

            // Request cancellation from the UI thread.
            if (Console.ReadKey().KeyChar == 'c')
            {
                tokenSource.Cancel();
                Console.WriteLine("\nTask cancellation requested.");

                // Optional: Observe the change in the Status property on the task.
                // It is not necessary to wait on tasks that have canceled. However,
                // if you do wait, you must enclose the call in a try-catch block to
                // catch the OperationCanceledExceptions that are thrown. If you do 
                // not wait, no OCE is thrown if the token that was passed to the 
                // StartNew method is the same token that requested the cancellation.

                #region Optional_WaitOnTasksToComplete
                try
                {
                    Task.WaitAll(tasks);
                }
                catch (AggregateException e)
                {
                    // For demonstration purposes, show the OCE message.
                    foreach (var v in e.InnerExceptions)
                        Console.WriteLine("msg: " + v.Message);
                }

                // Prove that the tasks are now all in a canceled state.
                for (int i = 0; i < tasks.Length; i++)
                    Console.WriteLine("task[{0}] status is now {1}", i, tasks[i].Status);
                #endregion
            }

            // Keep the console window open while the
            // task completes its output.
            Console.ReadLine();
        }

        static void DoSomeWork(int taskNum, CancellationToken ct)
        {
            // Was cancellation already requested?
            if (ct.IsCancellationRequested)
            {
                Console.WriteLine("We were cancelled before we got started.");
                Console.WriteLine("Press Enter to quit.");
                ct.ThrowIfCancellationRequested();
            }
            int maxIterations = 1000;

            // NOTE!!! A benign "OperationCanceledException was unhandled
            // by user code" error might be raised here. Press F5 to continue. Or,
            //  to avoid the error, uncheck the "Enable Just My Code"
            // option under Tools > Options > Debugging.
            for (int i = 0; i < maxIterations; i++)
            {
                // Do a bit of work. Not too much.
                var sw = new SpinWait();
                for (int j = 0; j < 3000; j++) sw.SpinOnce();
                Console.WriteLine("...{0} ", taskNum);
                if (ct.IsCancellationRequested)
                {
                    Console.WriteLine("bye from {0}.", taskNum);
                    Console.WriteLine("\nPress Enter to quit.");

                    ct.ThrowIfCancellationRequested();
                }
            }
        }
    }
}

System.Threading.Tasks.Task 클래스는 System.Threading.CancellationTokenSourceSystem.Threading.CancellationToken 형식을 기반으로 하는 취소 모델과 완전하게 통합됩니다. 자세한 내용은 취소작업 취소를 참조하십시오.

참고 항목

참조

System.Threading.CancellationTokenSource

System.Threading.CancellationToken

System.Threading.Tasks.Task

System.Threading.Tasks.Task<TResult>

개념

중첩된 작업 및 자식 작업

PLINQ 및 TPL의 람다 식

기타 리소스

작업 병렬 처리(작업 병렬 라이브러리)