Task 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
비동기 작업을 나타냅니다.
public ref class Task : IAsyncResult
public ref class Task : IAsyncResult, IDisposable
public class Task : IAsyncResult
public class Task : IAsyncResult, IDisposable
type Task = class
interface IAsyncResult
type Task = class
interface IAsyncResult
interface IDisposable
Public Class Task
Implements IAsyncResult
Public Class Task
Implements IAsyncResult, IDisposable
- 상속
-
Task
- 파생
- 구현
설명
클래스는 Task 값을 반환하지 않고 일반적으로 비동기적으로 실행되는 단일 작업을 나타냅니다. Task개체는 .NET Framework 4에서 처음 도입된 작업 기반 비동기 패턴의 중앙 구성 요소 중 하나입니다. 개체에서 Task 수행하는 작업은 일반적으로 기본 애플리케이션 스레드에서 동기적으로 실행되지 않고 스레드 풀 스레드에서 비동기적으로 실행되므로 속성과 IsCanceled, IsCompleted및 IsFaulted 속성을 사용하여 Status 작업의 상태를 확인할 수 있습니다. 가장 일반적으로 람다 식은 태스크가 수행할 작업을 지정하는 데 사용됩니다.
값을 반환하는 작업의 경우 클래스를 Task<TResult> 사용합니다.
이 섹션의 내용은 다음과 같습니다.
작업 인스턴스화 예제작업 만들기 및 실행 작업 만들기 및 실행 작업 만들기 및 실행 디버거 개발자를 위한 태스크 및 문화권을완료하기 위해 하나 이상의 태스크 대기
작업 인스턴스화
다음 예제에서는 4개의 작업을 만들고 실행합니다. 세 태스크는 형식Object의 Action<T> 인수를 허용하는 라는 action
대리자를 실행합니다. 네 번째 작업은 작업 만들기 메서드 호출에서 인라인으로 Action 정의된 람다 식(대리자)을 실행합니다. 각 작업은 인스턴스화되고 다른 방식으로 실행됩니다.
작업은
t1
Task 클래스 생성자를 호출하여 인스턴스화되지만 작업이 시작된 후에t2
만 메서드를 Start() 호출하여 시작됩니다.작업은
t2
인스턴스화되고 메서드를 호출 TaskFactory.StartNew(Action<Object>, Object) 하여 단일 메서드 호출에서 시작됩니다.작업은
t3
인스턴스화되고 메서드를 호출 Run(Action) 하여 단일 메서드 호출에서 시작됩니다.태스크
t4
는 메서드를 호출 RunSynchronously() 하여 기본 스레드에서 동기적으로 실행됩니다.
때문에 작업 t4
동기적으로 실행 애플리케이션 주 스레드에서 실행 됩니다. 나머지 작업은 일반적으로 하나 이상의 스레드 풀 스레드에서 비동기적으로 실행됩니다.
using System;
using System.Threading;
using System.Threading.Tasks;
class Example
{
static void Main()
{
Action<object> action = (object obj) =>
{
Console.WriteLine("Task={0}, obj={1}, Thread={2}",
Task.CurrentId, obj,
Thread.CurrentThread.ManagedThreadId);
};
// Create a task but do not start it.
Task t1 = new Task(action, "alpha");
// Construct a started task
Task t2 = Task.Factory.StartNew(action, "beta");
// Block the main thread to demonstrate that t2 is executing
t2.Wait();
// Launch t1
t1.Start();
Console.WriteLine("t1 has been launched. (Main Thread={0})",
Thread.CurrentThread.ManagedThreadId);
// Wait for the task to finish.
t1.Wait();
// Construct a started task using Task.Run.
String taskData = "delta";
Task t3 = Task.Run( () => {Console.WriteLine("Task={0}, obj={1}, Thread={2}",
Task.CurrentId, taskData,
Thread.CurrentThread.ManagedThreadId);
});
// Wait for the task to finish.
t3.Wait();
// Construct an unstarted task
Task t4 = new Task(action, "gamma");
// Run it synchronously
t4.RunSynchronously();
// Although the task was run synchronously, it is a good practice
// to wait for it in the event exceptions were thrown by the task.
t4.Wait();
}
}
// The example displays output like the following:
// Task=1, obj=beta, Thread=3
// t1 has been launched. (Main Thread=1)
// Task=2, obj=alpha, Thread=4
// Task=3, obj=delta, Thread=3
// Task=4, obj=gamma, Thread=1
open System.Threading
open System.Threading.Tasks
let action =
fun (obj: obj) -> printfn $"Task={Task.CurrentId}, obj={obj}, Thread={Thread.CurrentThread.ManagedThreadId}"
// Create a task but do not start it.
let t1 = new Task(action, "alpha")
// Construct a started task
let t2 = Task.Factory.StartNew(action, "beta")
// Block the main thread to demonstrate that t2 is executing
t2.Wait()
// Launch t1
t1.Start()
printfn $"t1 has been launched. (Main Thread={Thread.CurrentThread.ManagedThreadId})"
// Wait for the task to finish.
t1.Wait()
// Construct a started task using Task.Run.
let taskData = "delta"
let t3 =
Task.Run(fun () -> printfn $"Task={Task.CurrentId}, obj={taskData}, Thread={Thread.CurrentThread.ManagedThreadId}")
// Wait for the task to finish.
t3.Wait()
// Construct an unstarted task
let t4 = new Task(action, "gamma")
// Run it synchronously
t4.RunSynchronously()
// Although the task was run synchronously, it is a good practice
// to wait for it in the event exceptions were thrown by the task.
t4.Wait()
// The example displays output like the following:
// Task=1, obj=beta, Thread=3
// t1 has been launched. (Main Thread=1)
// Task=2, obj=alpha, Thread=4
// Task=3, obj=delta, Thread=3
// Task=4, obj=gamma, Thread=1
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim action As Action(Of Object) =
Sub(obj As Object)
Console.WriteLine("Task={0}, obj={1}, Thread={2}",
Task.CurrentId, obj,
Thread.CurrentThread.ManagedThreadId)
End Sub
' Construct an unstarted task
Dim t1 As New Task(action, "alpha")
' Construct a started task
Dim t2 As Task = Task.Factory.StartNew(action, "beta")
' Block the main thread to demonstrate that t2 is executing
t2.Wait()
' Launch t1
t1.Start()
Console.WriteLine("t1 has been launched. (Main Thread={0})",
Thread.CurrentThread.ManagedThreadId)
' Wait for the task to finish.
t1.Wait()
' Construct a started task using Task.Run.
Dim taskData As String = "delta"
Dim t3 As Task = Task.Run(Sub()
Console.WriteLine("Task={0}, obj={1}, Thread={2}",
Task.CurrentId, taskData,
Thread.CurrentThread.ManagedThreadId)
End Sub)
' Wait for the task to finish.
t3.Wait()
' Construct an unstarted task
Dim t4 As New Task(action, "gamma")
' Run it synchronously
t4.RunSynchronously()
' Although the task was run synchronously, it is a good practice
' to wait for it in the event exceptions were thrown by the task.
t4.Wait()
End Sub
End Module
' The example displays output like the following:
' Task=1, obj=beta, Thread=3
' t1 has been launched. (Main Thread=1)
' Task=2, obj=alpha, Thread=3
' Task=3, obj=delta, Thread=3
' Task=4, obj=gamma, Thread=1
작업 만들기 및 실행
Task 인스턴스는 다양한 방법으로 만들 수 있습니다. .NET Framework 4.5부터 사용할 수 있는 가장 일반적인 방법은 정적 Run 메서드를 호출하는 것입니다. 메서드는 Run 추가 매개 변수 없이 기본값을 사용하여 작업을 시작하는 간단한 방법을 제공합니다. 다음 예제에서는 메서드를 Run(Action) 사용하여 루프를 시작한 다음 루프 반복 수를 표시하는 작업을 시작합니다.
using System;
using System.Threading.Tasks;
public class Example
{
public static async Task Main()
{
await Task.Run( () => {
// Just loop.
int ctr = 0;
for (ctr = 0; ctr <= 1000000; ctr++)
{}
Console.WriteLine("Finished {0} loop iterations",
ctr);
} );
}
}
// The example displays the following output:
// Finished 1000001 loop iterations
open System.Threading.Tasks
let main =
task {
do!
Task.Run(fun () ->
for i = 0 to 1000000 do
printfn $"Finished {i} loop iterations")
}
main.Wait()
// The example displays the following output:
// Finished 1000001 loop iterations
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim t As Task = Task.Run(Sub()
' Just loop.
Dim ctr As Integer = 0
For ctr = 0 to 1000000
Next
Console.WriteLine("Finished {0} loop iterations",
ctr)
End Sub)
t.Wait()
End Sub
End Module
' The example displays the following output:
' Finished 1000001 loop iterations
.NET Framework 4에서 작업을 시작하는 가장 일반적인 방법인 대안은 정적 TaskFactory.StartNew 메서드입니다. 속성은 Task.Factory 개체를 TaskFactory 반환합니다. 메서드의 오버로드를 TaskFactory.StartNew 사용하면 작업 만들기 옵션 및 작업 스케줄러에 전달할 매개 변수를 지정할 수 있습니다. 다음 예제에서는 메서드를 TaskFactory.StartNew 사용하여 작업을 시작합니다. 이전 예제의 코드와 기능적으로 동일합니다.
using System;
using System.Threading.Tasks;
public class Example
{
public static void Main()
{
Task t = Task.Factory.StartNew( () => {
// Just loop.
int ctr = 0;
for (ctr = 0; ctr <= 1000000; ctr++)
{}
Console.WriteLine("Finished {0} loop iterations",
ctr);
} );
t.Wait();
}
}
// The example displays the following output:
// Finished 1000001 loop iterations
open System.Threading.Tasks
let t =
Task.Factory.StartNew(fun () ->
// Just loop.
for i = 0 to 1000000 do
printfn $"Finished {i} loop iterations")
t.Wait()
// The example displays the following output:
// Finished 1000001 loop iterations
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim t As Task = Task.Factory.StartNew(Sub()
' Just loop.
Dim ctr As Integer = 0
For ctr = 0 to 1000000
Next
Console.WriteLine("Finished {0} loop iterations",
ctr)
End Sub)
t.Wait()
End Sub
End Module
' The example displays the following output:
' Finished 1000001 loop iterations
자세한 예제는 작업 기반 비동기 프로그래밍을 참조하세요.
작업 만들기 및 실행 분리
또한 클래스는 Task 작업을 초기화하지만 실행하도록 예약하지 않는 생성자를 제공합니다. 성능상의 이유로 Task.Run 또는 TaskFactory.StartNew 메서드는 계산 작업을 만들고 예약하는 데 선호되는 메커니즘이지만 생성 및 예약을 구분해야 하는 시나리오의 경우 생성자를 사용한 다음 메서드를 호출 Task.Start 하여 나중에 실행할 작업을 예약할 수 있습니다.
하나 이상의 작업이 완료 될 때까지 대기 중
태스크는 일반적으로 스레드 풀 스레드에서 비동기적으로 실행되므로 태스크를 만들고 시작하는 스레드는 태스크가 인스턴스화되는 즉시 실행을 계속합니다. 경우에 따라 호출 스레드가 기본 애플리케이션 스레드인 경우 작업이 실제로 실행되기 전에 앱이 종료됩니다. 다른 작업에서는 하나 이상의 태스크가 실행을 완료한 경우에만 호출 스레드가 실행을 계속하도록 애플리케이션의 논리를 요구할 수 있습니다. 하나 이상의 작업이 완료되기를 기다리는 메서드를 호출 Wait
하여 호출 스레드의 실행과 실행 중인 비동기 작업을 동기화할 수 있습니다.
단일 작업이 완료되기를 기다리려면 해당 메서드를 호출할 Task.Wait 수 있습니다. 메서드에 대한 호출은 Wait 단일 클래스 instance 실행이 완료될 때까지 호출 스레드를 차단합니다.
다음 예제에서는 매개 변수가 없는 Wait() 메서드를 호출하여 작업이 완료될 때까지 무조건 대기합니다. 작업은 2초 동안 절전 모드로 메서드를 Thread.Sleep 호출하여 작업을 시뮬레이션합니다.
using System;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static Random rand = new Random();
static void Main()
{
// Wait on a single task with no timeout specified.
Task taskA = Task.Run( () => Thread.Sleep(2000));
Console.WriteLine("taskA Status: {0}", taskA.Status);
try {
taskA.Wait();
Console.WriteLine("taskA Status: {0}", taskA.Status);
}
catch (AggregateException) {
Console.WriteLine("Exception in taskA.");
}
}
}
// The example displays output like the following:
// taskA Status: WaitingToRun
// taskA Status: RanToCompletion
open System
open System.Threading
open System.Threading.Tasks
let rand = Random()
// Wait on a single task with no timeout specified.
let taskA = Task.Run(fun () -> Thread.Sleep 2000)
printfn $"taskA Status: {taskA.Status}"
try
taskA.Wait()
printfn $"taskA Status: {taskA.Status}"
with :? AggregateException ->
printfn "Exception in taskA."
// The example displays output like the following:
// taskA Status: WaitingToRun
// taskA Status: RanToCompletion
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
' Wait on a single task with no timeout specified.
Dim taskA = Task.Run( Sub() Thread.Sleep(2000))
Console.WriteLine("taskA Status: {0}", taskA.Status)
Try
taskA.Wait()
Console.WriteLine("taskA Status: {0}", taskA.Status)
Catch e As AggregateException
Console.WriteLine("Exception in taskA.")
End Try
End Sub
End Module
' The example displays output like the following:
' taskA Status: WaitingToRun
' taskA Status: RanToCompletion
작업이 완료되기를 조건부로 기다릴 수도 있습니다. 및 Wait(TimeSpan) 메서드는 Wait(Int32) 태스크가 완료되거나 시간 제한 간격이 경과할 때까지 호출 스레드를 차단합니다. 다음 예제에서는 2초 동안 절전 모드이지만 1초 시간 제한 값을 정의하는 작업을 실행하므로 호출 스레드는 시간 제한이 만료될 때까지 및 태스크 실행이 완료되기 전까지 차단됩니다.
using System;
using System.Threading;
using System.Threading.Tasks;
public class Example
{
public static void Main()
{
// Wait on a single task with a timeout specified.
Task taskA = Task.Run( () => Thread.Sleep(2000));
try {
taskA.Wait(1000); // Wait for 1 second.
bool completed = taskA.IsCompleted;
Console.WriteLine("Task A completed: {0}, Status: {1}",
completed, taskA.Status);
if (! completed)
Console.WriteLine("Timed out before task A completed.");
}
catch (AggregateException) {
Console.WriteLine("Exception in taskA.");
}
}
}
// The example displays output like the following:
// Task A completed: False, Status: Running
// Timed out before task A completed.
open System
open System.Threading
open System.Threading.Tasks
// Wait on a single task with a timeout specified.
let taskA = Task.Run(fun () -> Thread.Sleep 2000)
try
taskA.Wait 1000 |> ignore // Wait for 1 second.
let completed = taskA.IsCompleted
printfn $"Task A completed: {completed}, Status: {taskA.Status}"
if not completed then
printfn "Timed out before task A completed."
with :? AggregateException ->
printfn "Exception in taskA."
// The example displays output like the following:
// Task A completed: False, Status: Running
// Timed out before task A completed.
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
' Wait on a single task with a timeout specified.
Dim taskA As Task = Task.Run( Sub() Thread.Sleep(2000))
Try
taskA.Wait(1000) ' Wait for 1 second.
Dim completed As Boolean = taskA.IsCompleted
Console.WriteLine("Task.Completed: {0}, Status: {1}",
completed, taskA.Status)
If Not completed Then
Console.WriteLine("Timed out before task A completed.")
End If
Catch e As AggregateException
Console.WriteLine("Exception in taskA.")
End Try
End Sub
End Module
' The example displays the following output:
' Task A completed: False, Status: Running
' Timed out before task A completed.
및 Wait(Int32, CancellationToken) 메서드를 호출하여 취소 토큰을 Wait(CancellationToken) 제공할 수도 있습니다. 메서드가 실행되는 동안 토큰의 IsCancellationRequested 속성이 이거나 true
가 되면 true
메서드는 을 OperationCanceledExceptionthrow Wait 합니다.
경우에 따라 일련의 실행 중인 작업 중 첫 번째 작업이 완료되기를 기다리는 것이 좋지만 어떤 작업인지는 신경 쓰지 않을 수 있습니다. 이를 위해 메서드의 오버로드 중 하나를 호출할 Task.WaitAny 수 있습니다. 다음 예제에서는 세 가지 작업을 만듭니다. 각 작업은 난수 생성기에 의해 결정되는 간격 동안 절전 모드로 유지됩니다. 메서드는 WaitAny(Task[]) 첫 번째 작업이 완료되기를 기다립니다. 이 예제에서는 세 가지 작업 모두의 상태 대한 정보를 표시합니다.
using System;
using System.Threading;
using System.Threading.Tasks;
public class Example
{
public static void Main()
{
var tasks = new Task[3];
var rnd = new Random();
for (int ctr = 0; ctr <= 2; ctr++)
tasks[ctr] = Task.Run( () => Thread.Sleep(rnd.Next(500, 3000)));
try {
int index = Task.WaitAny(tasks);
Console.WriteLine("Task #{0} completed first.\n", tasks[index].Id);
Console.WriteLine("Status of all tasks:");
foreach (var t in tasks)
Console.WriteLine(" Task #{0}: {1}", t.Id, t.Status);
}
catch (AggregateException) {
Console.WriteLine("An exception occurred.");
}
}
}
// The example displays output like the following:
// Task #1 completed first.
//
// Status of all tasks:
// Task #3: Running
// Task #1: RanToCompletion
// Task #4: Running
open System
open System.Threading
open System.Threading.Tasks
let rnd = new Random()
let tasks =
[| for _ = 0 to 2 do
Task.Run(fun () -> rnd.Next(500, 3000) |> Thread.Sleep) |]
try
let index = Task.WaitAny tasks
printfn $"Task #{tasks[index].Id} completed first.\n"
printfn "Status of all tasks:"
for t in tasks do
printfn $" Task #{t.Id}: {t.Status}"
with :? AggregateException ->
printfn "An exception occurred."
// The example displays output like the following:
// Task #1 completed first.
//
// Status of all tasks:
// Task #3: Running
// Task #1: RanToCompletion
// Task #4: Running
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim tasks(2) As Task
Dim rnd As New Random()
For ctr As Integer = 0 To 2
tasks(ctr) = Task.Run( Sub() Thread.Sleep(rnd.Next(500, 3000)))
Next
Try
Dim index As Integer= Task.WaitAny(tasks)
Console.WriteLine("Task #{0} completed first.", tasks(index).Id)
Console.WriteLine()
Console.WriteLine("Status of all tasks:")
For Each t in tasks
Console.WriteLine(" Task #{0}: {1}", t.Id, t.Status)
Next
Catch e As AggregateException
Console.WriteLine("An exception occurred.")
End Try
End Sub
End Module
' The example displays output like the following:
' Task #1 completed first.
'
' Status of all tasks:
' Task #3: Running
' Task #1: RanToCompletion
' Task #4: Running
메서드를 호출 WaitAll 하여 일련의 모든 작업이 완료되기를 기다릴 수도 있습니다. 다음 예제에서는 10개의 작업을 만들고, 10개 작업이 모두 완료되기를 기다린 다음, 해당 상태 표시합니다.
using System;
using System.Threading;
using System.Threading.Tasks;
public class Example
{
public static void Main()
{
// Wait for all tasks to complete.
Task[] tasks = new Task[10];
for (int i = 0; i < 10; i++)
{
tasks[i] = Task.Run(() => Thread.Sleep(2000));
}
try {
Task.WaitAll(tasks);
}
catch (AggregateException ae) {
Console.WriteLine("One or more exceptions occurred: ");
foreach (var ex in ae.Flatten().InnerExceptions)
Console.WriteLine(" {0}", ex.Message);
}
Console.WriteLine("Status of completed tasks:");
foreach (var t in tasks)
Console.WriteLine(" Task #{0}: {1}", t.Id, t.Status);
}
}
// The example displays the following output:
// Status of completed tasks:
// Task #2: RanToCompletion
// Task #1: RanToCompletion
// Task #3: RanToCompletion
// Task #4: RanToCompletion
// Task #6: RanToCompletion
// Task #5: RanToCompletion
// Task #7: RanToCompletion
// Task #8: RanToCompletion
// Task #9: RanToCompletion
// Task #10: RanToCompletion
open System
open System.Threading
open System.Threading.Tasks
// Wait for all tasks to complete.
let tasks =
[| for _ = 0 to 9 do
Task.Run(fun () -> Thread.Sleep 2000) |]
try
Task.WaitAll tasks
with :? AggregateException as ae ->
printfn "One or more exceptions occurred: "
for ex in ae.Flatten().InnerExceptions do
printfn $" {ex.Message}"
printfn "Status of completed tasks:"
for t in tasks do
printfn $" Task #{t.Id}: {t.Status}"
// The example displays the following output:
// Status of completed tasks:
// Task #2: RanToCompletion
// Task #1: RanToCompletion
// Task #3: RanToCompletion
// Task #4: RanToCompletion
// Task #6: RanToCompletion
// Task #5: RanToCompletion
// Task #7: RanToCompletion
// Task #8: RanToCompletion
// Task #9: RanToCompletion
// Task #10: RanToCompletion
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
' Wait for all tasks to complete.
Dim tasks(9) As Task
For i As Integer = 0 To 9
tasks(i) = Task.Run( Sub() Thread.Sleep(2000) )
Next
Try
Task.WaitAll(tasks)
Catch ae As AggregateException
Console.WriteLine("One or more exceptions occurred: ")
For Each ex In ae.Flatten().InnerExceptions
Console.WriteLine(" {0}", ex.Message)
Next
End Try
Console.WriteLine("Status of completed tasks:")
For Each t in tasks
Console.WriteLine(" Task #{0}: {1}", t.Id, t.Status)
Next
End Sub
End Module
' The example displays the following output:
' Status of completed tasks:
' Task #2: RanToCompletion
' Task #1: RanToCompletion
' Task #3: RanToCompletion
' Task #4: RanToCompletion
' Task #6: RanToCompletion
' Task #5: RanToCompletion
' Task #7: RanToCompletion
' Task #8: RanToCompletion
' Task #9: RanToCompletion
' Task #10: RanToCompletion
하나 이상의 작업이 완료될 때까지 기다리면 다음 예제와 같이 실행 중인 태스크에서 throw된 모든 예외가 메서드를 호출 Wait
하는 스레드에서 전파됩니다. 12개의 태스크가 시작되며, 그 중 3개는 정상적으로 완료되고 그 중 3개는 예외를 throw합니다. 나머지 6개 작업 중 3개는 시작하기 전에 취소되고 3개는 실행 중 취소됩니다. 예외는 메서드 호출에서 WaitAll throw되고 블록에서 try
/catch
처리됩니다.
using System;
using System.Threading;
using System.Threading.Tasks;
public class Example
{
public static void Main()
{
// Create a cancellation token and cancel it.
var source1 = new CancellationTokenSource();
var token1 = source1.Token;
source1.Cancel();
// Create a cancellation token for later cancellation.
var source2 = new CancellationTokenSource();
var token2 = source2.Token;
// Create a series of tasks that will complete, be cancelled,
// timeout, or throw an exception.
Task[] tasks = new Task[12];
for (int i = 0; i < 12; i++)
{
switch (i % 4)
{
// Task should run to completion.
case 0:
tasks[i] = Task.Run(() => Thread.Sleep(2000));
break;
// Task should be set to canceled state.
case 1:
tasks[i] = Task.Run( () => Thread.Sleep(2000),
token1);
break;
case 2:
// Task should throw an exception.
tasks[i] = Task.Run( () => { throw new NotSupportedException(); } );
break;
case 3:
// Task should examine cancellation token.
tasks[i] = Task.Run( () => { Thread.Sleep(2000);
if (token2.IsCancellationRequested)
token2.ThrowIfCancellationRequested();
Thread.Sleep(500); }, token2);
break;
}
}
Thread.Sleep(250);
source2.Cancel();
try {
Task.WaitAll(tasks);
}
catch (AggregateException ae) {
Console.WriteLine("One or more exceptions occurred:");
foreach (var ex in ae.InnerExceptions)
Console.WriteLine(" {0}: {1}", ex.GetType().Name, ex.Message);
}
Console.WriteLine("\nStatus of tasks:");
foreach (var t in tasks) {
Console.WriteLine(" Task #{0}: {1}", t.Id, t.Status);
if (t.Exception != null) {
foreach (var ex in t.Exception.InnerExceptions)
Console.WriteLine(" {0}: {1}", ex.GetType().Name,
ex.Message);
}
}
}
}
// The example displays output like the following:
// One or more exceptions occurred:
// TaskCanceledException: A task was canceled.
// NotSupportedException: Specified method is not supported.
// TaskCanceledException: A task was canceled.
// TaskCanceledException: A task was canceled.
// NotSupportedException: Specified method is not supported.
// TaskCanceledException: A task was canceled.
// TaskCanceledException: A task was canceled.
// NotSupportedException: Specified method is not supported.
// TaskCanceledException: A task was canceled.
//
// Status of tasks:
// Task #13: RanToCompletion
// Task #1: Canceled
// Task #3: Faulted
// NotSupportedException: Specified method is not supported.
// Task #8: Canceled
// Task #14: RanToCompletion
// Task #4: Canceled
// Task #6: Faulted
// NotSupportedException: Specified method is not supported.
// Task #7: Canceled
// Task #15: RanToCompletion
// Task #9: Canceled
// Task #11: Faulted
// NotSupportedException: Specified method is not supported.
// Task #12: Canceled
open System
open System.Threading
open System.Threading.Tasks
// Create a cancellation token and cancel it.
let source1 = new CancellationTokenSource()
let token1 = source1.Token
source1.Cancel()
// Create a cancellation token for later cancellation.
let source2 = new CancellationTokenSource()
let token2 = source2.Token
// Create a series of tasks that will complete, be cancelled,
// timeout, or throw an exception.
let tasks =
[| for i in 0..11 do
match i % 4 with
// Task should run to completion.
| 0 -> Task.Run(fun () -> Thread.Sleep 2000)
// Task should be set to canceled state.
| 1 -> Task.Run(fun () -> Thread.Sleep 2000, token1)
// Task should throw an exception.
| 2 -> Task.Run(fun () -> NotSupportedException())
// Task should examine cancellation token.
| _ ->
Task.Run(fun () ->
Thread.Sleep 2000
if token2.IsCancellationRequested then
token2.ThrowIfCancellationRequested()
Thread.Sleep 500, token2) |]
Thread.Sleep 250
source2.Cancel()
try
Task.WaitAll tasks
with :? AggregateException as ae ->
printfn "One or more exceptions occurred:"
for ex in ae.InnerExceptions do
printfn $" {ex.GetType().Name}: {ex.Message}"
printfn "\nStatus of tasks:"
for t in tasks do
printfn $" Task #{t.Id}: {t.Status}"
if isNull t.Exception |> not then
for ex in t.Exception.InnerExceptions do
printfn $" {ex.GetType().Name}: {ex.Message}"
// The example displays output like the following:
// One or more exceptions occurred:
// TaskCanceledException: A task was canceled.
// NotSupportedException: Specified method is not supported.
// TaskCanceledException: A task was canceled.
// TaskCanceledException: A task was canceled.
// NotSupportedException: Specified method is not supported.
// TaskCanceledException: A task was canceled.
// TaskCanceledException: A task was canceled.
// NotSupportedException: Specified method is not supported.
// TaskCanceledException: A task was canceled.
//
// Status of tasks:
// Task #13: RanToCompletion
// Task #1: Canceled
// Task #3: Faulted
// NotSupportedException: Specified method is not supported.
// Task #8: Canceled
// Task #14: RanToCompletion
// Task #4: Canceled
// Task #6: Faulted
// NotSupportedException: Specified method is not supported.
// Task #7: Canceled
// Task #15: RanToCompletion
// Task #9: Canceled
// Task #11: Faulted
// NotSupportedException: Specified method is not supported.
// Task #12: Canceled
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Public Sub Main()
' Create a cancellation token and cancel it.
Dim source1 As New CancellationTokenSource()
Dim token1 As CancellationToken = source1.Token
source1.Cancel()
' Create a cancellation token for later cancellation.
Dim source2 As New CancellationTokenSource()
Dim token2 As CancellationToken = source2.Token
' Create a series of tasks that will complete, be cancelled,
' timeout, or throw an exception.
Dim tasks(11) As Task
For i As Integer = 0 To 11
Select Case i Mod 4
' Task should run to completion.
Case 0
tasks(i) = Task.Run( Sub() Thread.Sleep(2000))
' Task should be set to canceled state.
Case 1
tasks(i) = Task.Run( Sub() Thread.Sleep(2000), token1)
Case 2
' Task should throw an exception.
tasks(i) = Task.Run( Sub()
Throw New NotSupportedException()
End Sub)
Case 3
' Task should examine cancellation token.
tasks(i) = Task.Run( Sub()
Thread.Sleep(2000)
If token2.IsCancellationRequested
token2.ThrowIfCancellationRequested()
End If
Thread.Sleep(500)
End Sub, token2)
End Select
Next
Thread.Sleep(250)
source2.Cancel()
Try
Task.WaitAll(tasks)
Catch ae As AggregateException
Console.WriteLine("One or more exceptions occurred:")
For Each ex in ae.InnerExceptions
Console.WriteLine(" {0}: {1}", ex.GetType().Name, ex.Message)
Next
End Try
Console.WriteLine()
Console.WriteLine("Status of tasks:")
For Each t in tasks
Console.WriteLine(" Task #{0}: {1}", t.Id, t.Status)
If t.Exception IsNot Nothing Then
For Each ex in t.Exception.InnerExceptions
Console.WriteLine(" {0}: {1}", ex.GetType().Name,
ex.Message)
Next
End If
Next
End Sub
End Module
' The example displays output like the following:
' One or more exceptions occurred:
' TaskCanceledException: A task was canceled.
' NotSupportedException: Specified method is not supported.
' TaskCanceledException: A task was canceled.
' TaskCanceledException: A task was canceled.
' NotSupportedException: Specified method is not supported.
' TaskCanceledException: A task was canceled.
' TaskCanceledException: A task was canceled.
' NotSupportedException: Specified method is not supported.
' TaskCanceledException: A task was canceled.
'
' Status of tasks:
' Task #13: RanToCompletion
' Task #1: Canceled
' Task #3: Faulted
' NotSupportedException: Specified method is not supported.
' Task #8: Canceled
' Task #14: RanToCompletion
' Task #4: Canceled
' Task #6: Faulted
' NotSupportedException: Specified method is not supported.
' Task #7: Canceled
' Task #15: RanToCompletion
' Task #9: Canceled
' Task #11: Faulted
' NotSupportedException: Specified method is not supported.
' Task #12: Canceled
작업 기반 비동기 작업의 예외 처리에 대한 자세한 내용은 예외 처리를 참조하세요.
작업 및 문화권
.NET Framework 4.6을 대상으로 하는 데스크톱 앱부터 작업을 만들고 호출하는 스레드의 문화권은 스레드 컨텍스트의 일부가 됩니다. 즉, 태스크가 실행되는 스레드의 현재 문화권에 관계없이 태스크의 현재 문화권은 호출 스레드의 문화권입니다. .NET Framework 4.6 이전의 .NET Framework 버전을 대상으로 하는 앱의 경우 태스크의 문화권은 태스크가 실행되는 스레드의 문화권입니다. 자세한 내용은 항목의 "문화권 및 작업 기반 비동기 작업" 섹션을 CultureInfo 참조하세요.
참고
스토어 앱은 기본 문화권을 설정하고 가져오는 Windows 런타임 따릅니다.
디버거 개발자용
사용자 지정 디버거를 구현하는 개발자의 경우 작업의 여러 내부 및 프라이빗 멤버가 유용할 수 있습니다(릴리스에서 릴리스로 변경될 수 있음). m_taskId
필드는 속성의 Id 백업 저장소 역할을 하지만 디버거에서 직접 이 필드에 액세스하는 것이 속성의 getter 메서드를 통해 동일한 값에 액세스하는 것보다 더 효율적일 수 있습니다(s_taskIdCounter
카운터는 작업에 사용할 수 있는 다음 ID를 검색하는 데 사용됩니다). 마찬가지로 m_stateFlags
필드는 작업의 현재 수명 주기 단계에 대한 정보를 저장하고 속성을 통해 Status 정보에도 액세스할 수 있습니다. m_action
필드는 태스크의 대리자 참조를 저장하고 m_stateObject
필드는 개발자가 작업에 전달한 비동기 상태를 저장합니다. 마지막으로 스택 프레임을 구문 분석하는 디버거의 InternalWait
경우 메서드는 태스크가 대기 작업을 입력하는 경우에 대한 잠재적 마커를 제공합니다.
생성자
Task(Action) |
지정된 작업을 사용하여 새 Task를 초기화합니다. |
Task(Action, CancellationToken) |
지정된 작업을 사용하여 새 Task 및 CancellationToken을 초기화합니다. |
Task(Action, CancellationToken, TaskCreationOptions) |
지정된 작업 및 만들기 옵션을 사용하여 새 Task를 초기화합니다. |
Task(Action, TaskCreationOptions) |
지정된 작업 및 만들기 옵션을 사용하여 새 Task를 초기화합니다. |
Task(Action<Object>, Object) |
지정된 작업 및 상태를 사용하여 새 Task를 초기화합니다. |
Task(Action<Object>, Object, CancellationToken) |
지정된 작업, 상태 및 옵션을 사용하여 새 Task를 초기화합니다. |
Task(Action<Object>, Object, CancellationToken, TaskCreationOptions) |
지정된 작업, 상태 및 옵션을 사용하여 새 Task를 초기화합니다. |
Task(Action<Object>, Object, TaskCreationOptions) |
지정된 작업, 상태 및 옵션을 사용하여 새 Task를 초기화합니다. |
속성
AsyncState |
Task를 만들 때 제공된 상태 개체 또는 제공된 개체가 없는 경우 null을 가져옵니다. |
CompletedTask |
이미 성공적으로 완료된 작업을 가져옵니다. |
CreationOptions |
이 작업을 만드는 데 사용된 TaskCreationOptions를 가져옵니다. |
CurrentId |
현재 실행 중인 Task의 ID를 반환합니다. |
Exception |
AggregateException가 중간에 종료되도록 하는 Task을 가져옵니다. Task가 완료되었거나 예외를 아직 throw하지 않았을 경우 |
Factory |
Task 및 Task<TResult> 인스턴스를 만들고 구성하는 팩터리 메서드에 대한 액세스를 제공합니다. |
Id |
이 Task 인스턴스에 대한 ID를 가져옵니다. |
IsCanceled |
이 Task 인스턴스가 취소되어 실행을 완료했는지 여부를 가져옵니다. |
IsCompleted |
작업이 완료되었는지 여부를 나타내는 값을 가져옵니다. |
IsCompletedSuccessfully |
작업이 완료될 때까지 실행되었는지 여부를 가져옵니다. |
IsFaulted |
처리되지 않은 예외로 인해 Task가 완료되었는지 여부를 가져옵니다. |
Status |
이 작업의 TaskStatus를 가져옵니다. |
메서드
ConfigureAwait(Boolean) |
이 Task를 기다리는 데 사용되는 awaiter를 구성합니다. |
ConfigureAwait(ConfigureAwaitOptions) |
이 Task를 기다리는 데 사용되는 awaiter를 구성합니다. |
ContinueWith(Action<Task,Object>, Object) |
호출자 제공 상태 정보를 받으며 대상 Task이(가) 완료되면 실행되는 연속 작업을 만듭니다. |
ContinueWith(Action<Task,Object>, Object, CancellationToken) |
대상 Task가 완료될 때 호출자 제공 상태 정보 및 취소 토큰을 받고 비동기적으로 실행되는 연속 작업을 만듭니다. |
ContinueWith(Action<Task,Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) |
대상 Task가 완료될 때 호출자 제공 상태 정보 및 취소 토큰을 받고 실행되는 연속 작업을 만듭니다. 연속 작업은 지정된 조건의 집합에 따라 실행되며 지정된 스케줄러를 사용합니다. |
ContinueWith(Action<Task,Object>, Object, TaskContinuationOptions) |
호출자 제공 상태 정보를 받으며 대상 Task이(가) 완료되면 실행되는 연속 작업을 만듭니다. 연속 작업은 지정된 조건의 집합에 따라 실행됩니다. |
ContinueWith(Action<Task,Object>, Object, TaskScheduler) |
호출자 제공 상태 정보를 받으며 대상 Task이(가) 완료되면 비동기적으로 실행되는 연속 작업을 만듭니다. 연속 작업은 지정된 스케줄러를 사용합니다. |
ContinueWith(Action<Task>) |
대상 Task가 완료될 때 비동기적으로 실행되는 연속 작업을 만듭니다. |
ContinueWith(Action<Task>, CancellationToken) |
대상 Task가 완료될 때 취소 토큰을 받고 비동기적으로 실행되는 연속 작업을 만듭니다. |
ContinueWith(Action<Task>, CancellationToken, TaskContinuationOptions, TaskScheduler) |
지정된 TaskContinuationOptions에 따라 대상 작업이 완료되면 실행되는 연속 작업을 만듭니다. 연속 작업은 취소 토큰을 받고 지정된 스케줄러를 사용합니다. |
ContinueWith(Action<Task>, TaskContinuationOptions) |
지정된 TaskContinuationOptions에 따라 대상 작업이 완료되면 실행되는 연속 작업을 만듭니다. |
ContinueWith(Action<Task>, TaskScheduler) |
대상 Task가 완료될 때 비동기적으로 실행되는 연속 작업을 만듭니다. 연속 작업은 지정된 스케줄러를 사용합니다. |
ContinueWith<TResult>(Func<Task,Object,TResult>, Object) |
호출자 제공 상태 정보를 받으며 대상 Task이(가) 완료되고 값을 가져오면 비동기적으로 실행되는 연속 작업을 만듭니다. |
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, CancellationToken) |
대상 Task이(가) 완료되고 값을 가져오면 비동기적으로 실행되는 연속 작업을 만듭니다. 연속 작업은 호출자 제공한 상태 정보 및 취소 토큰을 받습니다. |
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) |
대상 Task가 완료되고 값을 반환하면 지정된 작업 연속 옵션에 따라 실행되는 연속 작업을 만듭니다. 연속 작업은 호출자 제공 상태 정보 및 취소 토큰을 받고 지정된 스케줄러를 사용합니다. |
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, TaskContinuationOptions) |
대상 Task가 완료되면 지정된 작업 연속 옵션에 따라 실행되는 연속 작업을 만듭니다. 연속 작업은 호출자 제공 상태 정보를 받습니다. |
ContinueWith<TResult>(Func<Task,Object,TResult>, Object, TaskScheduler) |
대상 Task가 완료될 때 비동기적으로 실행되는 연속 작업을 만듭니다. 연속 작업은 호출자 제공 상태 정보를 받고 지정된 스케줄러를 사용합니다. |
ContinueWith<TResult>(Func<Task,TResult>) |
대상 Task<TResult>이(가) 완료되고 값을 가져오면 비동기적으로 실행되는 연속 작업을 만듭니다. |
ContinueWith<TResult>(Func<Task,TResult>, CancellationToken) |
대상 Task이(가) 완료되고 값을 가져오면 비동기적으로 실행되는 연속 작업을 만듭니다. 연속 작업은 취소 토큰을 받습니다. |
ContinueWith<TResult>(Func<Task,TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler) |
지정된 연속 작업 옵션에 따라 실행되고 값을 반환하는 연속 작업을 만듭니다. 연속 작업에 취소 토큰이 전달되고, 연속 작업은 지정된 스케줄러를 사용합니다. |
ContinueWith<TResult>(Func<Task,TResult>, TaskContinuationOptions) |
지정된 연속 작업 옵션에 따라 실행되고 값을 반환하는 연속 작업을 만듭니다. |
ContinueWith<TResult>(Func<Task,TResult>, TaskScheduler) |
대상 Task이(가) 완료되고 값을 가져오면 비동기적으로 실행되는 연속 작업을 만듭니다. 연속 작업은 지정된 스케줄러를 사용합니다. |
Delay(Int32) |
지정된 시간(밀리초) 후에 완료되는 작업을 만듭니다. |
Delay(Int32, CancellationToken) |
지정된 시간(밀리초) 후에 완료되는 취소 가능한 작업을 만듭니다. |
Delay(TimeSpan) |
지정된 시간 간격 후 완료되는 작업을 만듭니다. |
Delay(TimeSpan, CancellationToken) |
지정된 시간 간격 후 완료되는 취소 가능 작업을 만듭니다. |
Delay(TimeSpan, TimeProvider) |
지정된 시간 간격 후 완료되는 작업을 만듭니다. |
Delay(TimeSpan, TimeProvider, CancellationToken) |
지정된 시간 간격 후 완료되는 취소 가능 작업을 만듭니다. |
Dispose() |
Task 클래스의 현재 인스턴스에서 사용하는 모든 리소스를 해제합니다. |
Dispose(Boolean) |
Task를 삭제하고 관리되지 않는 해당 리소스를 모두 해제합니다. |
Equals(Object) |
지정된 개체가 현재 개체와 같은지 확인합니다. (다음에서 상속됨 Object) |
FromCanceled(CancellationToken) |
지정된 취소 토큰을 사용하여 취소로 인해 완료된 Task를 만듭니다. |
FromCanceled<TResult>(CancellationToken) |
지정된 취소 토큰을 사용하여 취소로 인해 완료된 Task<TResult>를 만듭니다. |
FromException(Exception) |
지정된 예외를 사용하여 완료된 Task를 만듭니다. |
FromException<TResult>(Exception) |
지정된 예외를 사용하여 완료된 Task<TResult>을 만듭니다. |
FromResult<TResult>(TResult) |
지정된 결과로 성공적으로 완료되는 Task<TResult>을 만듭니다. |
GetAwaiter() |
이 Task를 기다리는 데 사용되는 awaiter를 가져옵니다. |
GetHashCode() |
기본 해시 함수로 작동합니다. (다음에서 상속됨 Object) |
GetType() |
현재 인스턴스의 Type을 가져옵니다. (다음에서 상속됨 Object) |
MemberwiseClone() |
현재 Object의 단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
Run(Action) |
지정한 작업을 스레드 풀에서 실행하도록 큐에 대기시키고 작업을 나타내는 Task 개체를 반환합니다. |
Run(Action, CancellationToken) |
지정한 작업을 스레드 풀에서 실행하도록 큐에 대기시키고 작업을 나타내는 Task 개체를 반환합니다. 취소 토큰을 사용하면 작업이 아직 시작되지 않은 경우 작업을 취소할 수 있습니다. |
Run(Func<Task>) |
지정한 작업을 스레드 풀에서 실행하도록 큐에 대기시키고 |
Run(Func<Task>, CancellationToken) |
지정한 작업을 스레드 풀에서 실행하도록 큐에 대기시키고 |
Run<TResult>(Func<Task<TResult>>) |
지정된 작업을 스레드 풀에서 실행하도록 큐에 대기시키고 |
Run<TResult>(Func<Task<TResult>>, CancellationToken) |
지정된 작업을 스레드 풀에서 실행하도록 큐에 대기시키고 |
Run<TResult>(Func<TResult>) |
지정한 작업을 스레드 풀에서 실행하도록 큐에 대기시키고 작업을 나타내는 Task<TResult> 개체를 반환합니다. 취소 토큰을 사용하면 작업이 아직 시작되지 않은 경우 작업을 취소할 수 있습니다. |
Run<TResult>(Func<TResult>, CancellationToken) |
지정한 작업을 스레드 풀에서 실행하도록 큐에 대기시키고 작업을 나타내는 |
RunSynchronously() |
현재 Task에서 TaskScheduler를 동기적으로 실행합니다. |
RunSynchronously(TaskScheduler) |
제공된 Task에서 TaskScheduler를 동기적으로 실행합니다. |
Start() |
Task를 시작하고 현재 TaskScheduler에 실행을 예약합니다. |
Start(TaskScheduler) |
Task를 시작하고 지정된 TaskScheduler에 실행을 예약합니다. |
ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |
Wait() |
Task의 실행이 완료되기를 기다립니다. |
Wait(CancellationToken) |
Task의 실행이 완료되기를 기다립니다. 작업이 완료되기 전에 취소 토큰이 취소되면 대기가 종료됩니다. |
Wait(Int32) |
Task가 지정된 시간(밀리초) 내에 실행을 완료할 때까지 기다립니다. |
Wait(Int32, CancellationToken) |
Task의 실행이 완료되기를 기다립니다. 작업이 완료되기 전에 취소 토큰이 취소되었거나 시간 제한 간격이 경과되었으면 대기가 종료됩니다. |
Wait(TimeSpan) |
Task가 지정된 시간 간격 내에 실행을 완료할 때까지 기다립니다. |
Wait(TimeSpan, CancellationToken) |
Task의 실행이 완료되기를 기다립니다. |
WaitAll(Task[]) |
제공된 모든 Task 개체의 실행이 완료되기를 기다립니다. |
WaitAll(Task[], CancellationToken) |
대기가 취소되지 않는 경우 제공된 모든 Task 개체가 실행을 완료하기를 기다립니다. |
WaitAll(Task[], Int32) |
모든 제공된 Task 개체가 지정된 시간(밀리초) 내에 실행을 완료할 때까지 기다립니다. |
WaitAll(Task[], Int32, CancellationToken) |
제공된 모든 Task 개체가 지정된 시간(밀리초) 내에 실행을 완료하기를 기다리거나 대기가 취소될 때까지 기다립니다. |
WaitAll(Task[], TimeSpan) |
모든 제공된 취소 가능한 Task 개체가 지정된 시간 간격 내에 실행을 완료할 때까지 기다립니다. |
WaitAny(Task[]) |
제공된 Task 개체 중 임의 개체의 실행이 완료되기를 기다립니다. |
WaitAny(Task[], CancellationToken) |
대기가 취소되지 않는 경우 제공된 모든 Task 개체가 실행을 완료하기를 기다립니다. |
WaitAny(Task[], Int32) |
모든 제공된 Task 개체가 지정된 시간(밀리초) 내에 실행을 완료할 때까지 기다립니다. |
WaitAny(Task[], Int32, CancellationToken) |
모든 제공된 Task 개체가 지정된 시간(밀리초) 내에 실행을 완료하기를 기다리거나 취소 토큰이 취소될 때까지 기다립니다. |
WaitAny(Task[], TimeSpan) |
모든 제공된 Task 개체가 지정된 시간 간격 내에 실행을 완료할 때까지 기다립니다. |
WaitAsync(CancellationToken) |
Task 이 Task 작업이 완료되거나 지정된 에 취소가 요청되었을 때 완료되는 을 CancellationToken 가져옵니다. |
WaitAsync(TimeSpan) | |
WaitAsync(TimeSpan, CancellationToken) |
Task 이 Task 작업이 완료될 때, 지정된 시간 제한이 만료될 때 또는 지정된 에 취소가 요청된 경우 완료되는 을 CancellationToken 가져옵니다. |
WaitAsync(TimeSpan, TimeProvider) | |
WaitAsync(TimeSpan, TimeProvider, CancellationToken) |
Task 이 Task 작업이 완료될 때, 지정된 시간 제한이 만료될 때 또는 지정된 에 취소가 요청된 경우 완료되는 을 CancellationToken 가져옵니다. |
WhenAll(IEnumerable<Task>) |
열거 가능한 컬렉션의 모든 Task 개체가 완료되면 완료될 작업을 만듭니다. |
WhenAll(Task[]) |
배열의 모든 Task 개체가 완료되면 완료될 작업을 만듭니다. |
WhenAll<TResult>(IEnumerable<Task<TResult>>) |
열거 가능한 컬렉션의 모든 Task<TResult> 개체가 완료되면 완료될 작업을 만듭니다. |
WhenAll<TResult>(Task<TResult>[]) |
배열의 모든 Task<TResult> 개체가 완료되면 완료될 작업을 만듭니다. |
WhenAny(IEnumerable<Task>) |
제공된 작업을 모두 완료했을 때 완료할 작업을 만듭니다. |
WhenAny(Task, Task) |
제공된 작업 중 하나가 완료했을 때 완료할 작업을 만듭니다. |
WhenAny(Task[]) |
제공된 작업을 모두 완료했을 때 완료할 작업을 만듭니다. |
WhenAny<TResult>(IEnumerable<Task<TResult>>) |
제공된 작업을 모두 완료했을 때 완료할 작업을 만듭니다. |
WhenAny<TResult>(Task<TResult>, Task<TResult>) |
제공된 작업 중 하나가 완료했을 때 완료할 작업을 만듭니다. |
WhenAny<TResult>(Task<TResult>[]) |
제공된 작업을 모두 완료했을 때 완료할 작업을 만듭니다. |
Yield() |
대기할 때 현재 컨텍스트로 비동기적으로 전환되는 awaitable 작업을 만듭니다. |
명시적 인터페이스 구현
IAsyncResult.AsyncWaitHandle |
작업이 완료되기를 기다리는 데 사용할 수 있는 WaitHandle을 가져옵니다. |
IAsyncResult.CompletedSynchronously |
작업이 동기적으로 완료되었는지 여부를 나타내는 표시를 가져옵니다. |
확장 메서드
DispatcherOperationWait(Task) |
기본 DispatcherOperation이 완료되기를 무기한 기다립니다. |
DispatcherOperationWait(Task, TimeSpan) |
그런 다음 지정된 시간 동안 기본 DispatcherOperation이 완료되기를 기다립니다. |
IsDispatcherOperationTask(Task) |
이 Task가 DispatcherOperation과 연결되어 있는지 여부를 나타내는 값을 반환합니다. |
AsAsyncAction(Task) |
시작된 작업을 나타내는 Windows 런타임 비동기 작업을 반환합니다. |
적용 대상
스레드 보안
를 제외한 Dispose()의 Task모든 멤버는 스레드로부터 안전하며 여러 스레드에서 동시에 사용할 수 있습니다.