Task.WaitAll 메서드
정의
오버로드
WaitAll(Task[]) |
제공된 모든 Task 개체의 실행이 완료되기를 기다립니다.Waits for all of the provided Task objects to complete execution. |
WaitAll(Task[], Int32) |
모든 제공된 Task 개체가 지정된 시간(밀리초) 내에 실행을 완료할 때까지 기다립니다.Waits for all of the provided Task objects to complete execution within a specified number of milliseconds. |
WaitAll(Task[], CancellationToken) |
대기가 취소되지 않는 경우 제공된 모든 Task 개체가 실행을 완료하기를 기다립니다.Waits for all of the provided Task objects to complete execution unless the wait is cancelled. |
WaitAll(Task[], TimeSpan) |
모든 제공된 취소 가능한 Task 개체가 지정된 시간 간격 내에 실행을 완료할 때까지 기다립니다.Waits for all of the provided cancellable Task objects to complete execution within a specified time interval. |
WaitAll(Task[], Int32, CancellationToken) |
제공된 모든 Task 개체가 지정된 시간(밀리초) 내에 실행을 완료하기를 기다리거나 대기가 취소될 때까지 기다립니다.Waits for all of the provided Task objects to complete execution within a specified number of milliseconds or until the wait is cancelled. |
WaitAll(Task[])
public:
static void WaitAll(... cli::array <System::Threading::Tasks::Task ^> ^ tasks);
public static void WaitAll (params System.Threading.Tasks.Task[] tasks);
static member WaitAll : System.Threading.Tasks.Task[] -> unit
Public Shared Sub WaitAll (ParamArray tasks As Task())
매개 변수
예외
tasks
인수가 null
인 경우The tasks
argument is null
.
tasks
인수에 null 요소가 포함되어 있습니다.The tasks
argument contains a null element.
Task 인스턴스 중 하나 이상이 취소된 경우At least one of the Task instances was canceled. 작업이 취소된 경우 AggregateException 예외에는 해당 InnerExceptions 컬렉션의 OperationCanceledException 예외가 포함됩니다.If a task was canceled, the AggregateException exception contains an OperationCanceledException exception in its InnerExceptions collection.
또는-or- Task 인스턴스 중 하나 이상을 실행하는 동안 예외가 발생한 경우An exception was thrown during the execution of at least one of the Task instances.
예제
다음 예제에서는 각각 상태 개체로 인덱스를 전달 하는 10 개의 작업을 시작 합니다.The following example starts 10 tasks, each of which is passed an index as a state object. 인덱스를 사용 하는 작업은 예외를 throw 합니다.Tasks with an index from two to five throw exceptions. 메서드를 호출 하면 WaitAll 개체의 모든 예외가 래핑하고 AggregateException 호출 스레드로 전파 됩니다.The call to the WaitAll method wraps all exceptions in an AggregateException object and propagates it to the calling thread.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
class Example
{
static void Main()
{
var tasks = new List<Task<int>>();
// Define a delegate that prints and returns the system tick count
Func<object, int> action = (object obj) =>
{
int i = (int)obj;
// Make each thread sleep a different time in order to return a different tick count
Thread.Sleep(i * 100);
// The tasks that receive an argument between 2 and 5 throw exceptions
if (2 <= i && i <= 5)
{
throw new InvalidOperationException("SIMULATED EXCEPTION");
}
int tickCount = Environment.TickCount;
Console.WriteLine("Task={0}, i={1}, TickCount={2}, Thread={3}", Task.CurrentId, i, tickCount, Thread.CurrentThread.ManagedThreadId);
return tickCount;
};
// Construct started tasks
for (int i = 0; i < 10; i++)
{
int index = i;
tasks.Add(Task<int>.Factory.StartNew(action, index));
}
try
{
// Wait for all the tasks to finish.
Task.WaitAll(tasks.ToArray());
// We should never get to this point
Console.WriteLine("WaitAll() has not thrown exceptions. THIS WAS NOT EXPECTED.");
}
catch (AggregateException e)
{
Console.WriteLine("\nThe following exceptions have been thrown by WaitAll(): (THIS WAS EXPECTED)");
for (int j = 0; j < e.InnerExceptions.Count; j++)
{
Console.WriteLine("\n-------------------------------------------------\n{0}", e.InnerExceptions[j].ToString());
}
}
}
}
// The example displays output like the following:
// Task=1, i=0, TickCount=1203822250, Thread=3
// Task=2, i=1, TickCount=1203822359, Thread=4
// Task=7, i=6, TickCount=1203823484, Thread=3
// Task=8, i=7, TickCount=1203823890, Thread=4
// Task=9, i=8, TickCount=1203824296, Thread=3
// Task=10, i=9, TickCount=1203824796, Thread=4
//
// The following exceptions have been thrown by WaitAll(): (THIS WAS EXPECTED)
//
// -------------------------------------------------
// System.InvalidOperationException: SIMULATED EXCEPTION
// at Example.<Main>b__0(Object obj)
// at System.Threading.Tasks.Task`1.InnerInvoke()
// at System.Threading.Tasks.Task.Execute()
//
// -------------------------------------------------
// System.InvalidOperationException: SIMULATED EXCEPTION
// at Example.<Main>b__0(Object obj)
// at System.Threading.Tasks.Task`1.InnerInvoke()
// at System.Threading.Tasks.Task.Execute()
//
// -------------------------------------------------
// System.InvalidOperationException: SIMULATED EXCEPTION
// at Example.<Main>b__0(Object obj)
// at System.Threading.Tasks.Task`1.InnerInvoke()
// at System.Threading.Tasks.Task.Execute()
//
// -------------------------------------------------
// System.InvalidOperationException: SIMULATED EXCEPTION
// at Example.<Main>b__0(Object obj)
// at System.Threading.Tasks.Task`1.InnerInvoke()
// at System.Threading.Tasks.Task.Execute()
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks
Module WaitAllDemo
Sub Main()
Dim tasks As New List(Of Task(Of Integer))()
' Define a delegate that prints and returns the system tick count
Dim action As Func(Of Object, Integer) = Function(obj As Object)
Dim i As Integer = CInt(obj)
' Make each thread sleep a different time in order to return a different tick count
Thread.Sleep(i * 100)
' The tasks that receive an argument between 2 and 5 throw exceptions
If 2 <= i AndAlso i <= 5 Then
Throw New InvalidOperationException("SIMULATED EXCEPTION")
End If
Dim tickCount As Integer = Environment.TickCount
Console.WriteLine("Task={0}, i={1}, TickCount={2}, Thread={3}", Task.CurrentId, i, tickCount, Thread.CurrentThread.ManagedThreadId)
Return tickCount
End Function
' Construct started tasks
For i As Integer = 0 To 9
Dim index As Integer = i
tasks.Add(Task(Of Integer).Factory.StartNew(action, index))
Next
Try
' Wait for all the tasks to finish.
Task.WaitAll(tasks.ToArray())
' We should never get to this point
Console.WriteLine("WaitAll() has not thrown exceptions. THIS WAS NOT EXPECTED.")
Catch e As AggregateException
Console.WriteLine(vbLf & "The following exceptions have been thrown by WaitAll(): (THIS WAS EXPECTED)")
For j As Integer = 0 To e.InnerExceptions.Count - 1
Console.WriteLine(vbLf & "-------------------------------------------------" & vbLf & "{0}", e.InnerExceptions(j).ToString())
Next
End Try
End Sub
End Module
' The example displays output like the following:
' Task=1, i=0, TickCount=1203822250, Thread=3
' Task=2, i=1, TickCount=1203822359, Thread=4
' Task=7, i=6, TickCount=1203823484, Thread=3
' Task=8, i=7, TickCount=1203823890, Thread=4
' Task=9, i=8, TickCount=1203824296, Thread=3
' Task=10, i=9, TickCount=1203824796, Thread=4
'
' The following exceptions have been thrown by WaitAll(): (THIS WAS EXPECTED)
'
' -------------------------------------------------
' System.InvalidOperationException: SIMULATED EXCEPTION
' at Example.<Main>b__0(Object obj)
' at System.Threading.Tasks.Task`1.InnerInvoke()
' at System.Threading.Tasks.Task.Execute()
'
' -------------------------------------------------
' System.InvalidOperationException: SIMULATED EXCEPTION
' at Example.<Main>b__0(Object obj)
' at System.Threading.Tasks.Task`1.InnerInvoke()
' at System.Threading.Tasks.Task.Execute()
'
' -------------------------------------------------
' System.InvalidOperationException: SIMULATED EXCEPTION
' at Example.<Main>b__0(Object obj)
' at System.Threading.Tasks.Task`1.InnerInvoke()
' at System.Threading.Tasks.Task.Execute()
'
' -------------------------------------------------
' System.InvalidOperationException: SIMULATED EXCEPTION
' at Example.<Main>b__0(Object obj)
' at System.Threading.Tasks.Task`1.InnerInvoke()
' at System.Threading.Tasks.Task.Execute()
적용 대상
WaitAll(Task[], Int32)
public:
static bool WaitAll(cli::array <System::Threading::Tasks::Task ^> ^ tasks, int millisecondsTimeout);
public static bool WaitAll (System.Threading.Tasks.Task[] tasks, int millisecondsTimeout);
static member WaitAll : System.Threading.Tasks.Task[] * int -> bool
Public Shared Function WaitAll (tasks As Task(), millisecondsTimeout As Integer) As Boolean
매개 변수
- millisecondsTimeout
- Int32
대기할 시간(밀리초)이거나, 무기한 대기할 경우 Infinite(-1)입니다.The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.
반환
모든 true
인스턴스의 실행이 할당된 시간 안에 완료되었으면 Task이고, 그렇지 않으면 false
입니다.true
if all of the Task instances completed execution within the allotted time; otherwise, false
.
예외
tasks
인수가 null
인 경우The tasks
argument is null
.
Task 인스턴스 중 하나 이상이 취소된 경우At least one of the Task instances was canceled. 작업이 취소된 경우 AggregateException의 InnerExceptions 컬렉션에 OperationCanceledException이 포함됩니다.If a task was canceled, the AggregateException contains an OperationCanceledException in its InnerExceptions collection.
또는-or- Task 인스턴스 중 하나 이상을 실행하는 동안 예외가 발생한 경우An exception was thrown during the execution of at least one of the Task instances.
millisecondsTimeout
이 시간 제한 없음을 나타내는 -1 이외의 음수인 경우millisecondsTimeout
is a negative number other than -1, which represents an infinite time-out.
tasks
인수에 null 요소가 포함되어 있습니다.The tasks
argument contains a null element.
적용 대상
WaitAll(Task[], CancellationToken)
public:
static void WaitAll(cli::array <System::Threading::Tasks::Task ^> ^ tasks, System::Threading::CancellationToken cancellationToken);
public static void WaitAll (System.Threading.Tasks.Task[] tasks, System.Threading.CancellationToken cancellationToken);
static member WaitAll : System.Threading.Tasks.Task[] * System.Threading.CancellationToken -> unit
Public Shared Sub WaitAll (tasks As Task(), cancellationToken As CancellationToken)
매개 변수
- cancellationToken
- CancellationToken
작업이 완료되기를 기다리는 동안 확인할 CancellationToken입니다.A CancellationToken to observe while waiting for the tasks to complete.
예외
cancellationToken
을 취소했습니다.The cancellationToken
was canceled.
tasks
인수가 null
인 경우The tasks
argument is null
.
Task 인스턴스 중 하나 이상이 취소된 경우At least one of the Task instances was canceled. 작업이 취소된 경우 AggregateException의 InnerExceptions 컬렉션에 OperationCanceledException이 포함됩니다.If a task was canceled, the AggregateException contains an OperationCanceledException in its InnerExceptions collection.
또는-or- Task 인스턴스 중 하나 이상을 실행하는 동안 예외가 발생한 경우An exception was thrown during the execution of at least one of the Task instances.
tasks
인수에 null 요소가 포함되어 있습니다.The tasks
argument contains a null element.
설명
cancellationToken
인수는 대기 작업을 취소 하는 데 사용 됩니다.The cancellationToken
argument is used to cancel the wait operation. 작업 취소는 별개의 작업 이며 위에서 설명한 대로에서 신호를 받을 수 AggregateException 있습니다.Cancellation of the tasks is a distinct operation, and is signaled by the AggregateException as noted above.
적용 대상
WaitAll(Task[], TimeSpan)
public:
static bool WaitAll(cli::array <System::Threading::Tasks::Task ^> ^ tasks, TimeSpan timeout);
public static bool WaitAll (System.Threading.Tasks.Task[] tasks, TimeSpan timeout);
static member WaitAll : System.Threading.Tasks.Task[] * TimeSpan -> bool
Public Shared Function WaitAll (tasks As Task(), timeout As TimeSpan) As Boolean
매개 변수
- timeout
- TimeSpan
대기할 시간(밀리초)을 나타내는 TimeSpan이거나, 무한 대기하도록 -1밀리초를 나타내는 TimeSpan입니다.A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely.
반환
모든 true
인스턴스의 실행이 할당된 시간 안에 완료되었으면 Task이고, 그렇지 않으면 false
입니다.true
if all of the Task instances completed execution within the allotted time; otherwise, false
.
예외
tasks
인수가 null
인 경우The tasks
argument is null
.
Task 인스턴스 중 하나 이상이 취소된 경우At least one of the Task instances was canceled. 작업이 취소된 경우 AggregateException의 InnerExceptions 컬렉션에 OperationCanceledException이 포함됩니다.If a task was canceled, the AggregateException contains an OperationCanceledException in its InnerExceptions collection.
또는-or- Task 인스턴스 중 하나 이상을 실행하는 동안 예외가 발생한 경우An exception was thrown during the execution of at least one of the Task instances.
timeout
은 시간 제한이 없음을 나타내는 -1밀리초 이외의 음수입니다.timeout
is a negative number other than -1 milliseconds, which represents an infinite time-out.
또는-or-
timeout
가 MaxValue보다 큰 경우timeout
is greater than MaxValue.
tasks
인수에 null 요소가 포함되어 있습니다.The tasks
argument contains a null element.
적용 대상
WaitAll(Task[], Int32, CancellationToken)
public:
static bool WaitAll(cli::array <System::Threading::Tasks::Task ^> ^ tasks, int millisecondsTimeout, System::Threading::CancellationToken cancellationToken);
public static bool WaitAll (System.Threading.Tasks.Task[] tasks, int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);
static member WaitAll : System.Threading.Tasks.Task[] * int * System.Threading.CancellationToken -> bool
Public Shared Function WaitAll (tasks As Task(), millisecondsTimeout As Integer, cancellationToken As CancellationToken) As Boolean
매개 변수
- millisecondsTimeout
- Int32
대기할 시간(밀리초)이거나, 무기한 대기할 경우 Infinite (-1)입니다.The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.
- cancellationToken
- CancellationToken
작업이 완료되기를 기다리는 동안 확인할 CancellationToken입니다.A CancellationToken to observe while waiting for the tasks to complete.
반환
모든 true
인스턴스의 실행이 할당된 시간 안에 완료되었으면 Task이고, 그렇지 않으면 false
입니다.true
if all of the Task instances completed execution within the allotted time; otherwise, false
.
예외
tasks
인수가 null
인 경우The tasks
argument is null
.
Task 인스턴스 중 하나 이상이 취소된 경우At least one of the Task instances was canceled. 작업이 취소된 경우 AggregateException의 InnerExceptions 컬렉션에 OperationCanceledException이 포함됩니다.If a task was canceled, the AggregateException contains an OperationCanceledException in its InnerExceptions collection.
또는-or- Task 인스턴스 중 하나 이상을 실행하는 동안 예외가 발생한 경우An exception was thrown during the execution of at least one of the Task instances.
millisecondsTimeout
이 시간 제한 없음을 나타내는 -1 이외의 음수인 경우millisecondsTimeout
is a negative number other than -1, which represents an infinite time-out.
tasks
인수에 null 요소가 포함되어 있습니다.The tasks
argument contains a null element.
cancellationToken
을 취소했습니다.The cancellationToken
was canceled.
설명
cancellationToken
인수는 대기 작업을 취소 하는 데 사용 됩니다.The cancellationToken
argument is used to cancel the wait operation. 작업 취소는 별개의 작업 이며 위에 설명 된로 신호를 전달 합니다 AggregateException .Cancellation of the tasks is a distinct operation, and is signaled by the AggregateException noted above.