방법: 대기 핸들이 있는 취소 요청 수신 대기
메서드가 신호를 받을 이벤트를 기다리는 중에 차단될 경우 해당 메시지는 컬렉션 토큰의 값을 확인하지 못하기 때문에 적시에 응답할 수 없습니다. 첫 번째 예제에서는 기본적으로 통합 취소 프레임워크를 지원하지 않는 System.Threading.ManualResetEvent 같은 이벤트를 사용하여 작업할 때 이 문제를 해결하는 방법을 보여 줍니다. 두 번째 예제에서는 통합 취소를 지원하지 않는 System.Threading.ManualResetEventSlim을 사용하는 더 효율적인 방법을 보여 줍니다.
참고 |
---|
"내 코드만"을 사용하는 경우 Visual Studio에서는 예외를 throw하는 줄에서 실행을 중단하고 예외가 사용자 코드를 통해 처리되지 않았음을 알리는 오류 메시지를 표시할 수 있습니다. 이는 크게 심각한 오류는 아닙니다.F5 키를 눌러 중단된 지점부터 실행을 계속하고 아래 예제에 나와 있는 것과 같은 예외 처리 동작을 확인할 수 있습니다.맨 처음 오류 지점에서 Visual Studio가 실행을 중단하지 않도록 하려면 도구, 옵션, 디버깅, 일반을 차례로 선택하고 "내 코드만" 확인란의 선택을 취소하기만 하면 됩니다. |
예제
다음 예제에서는 ManualResetEvent를 사용하여 통합 취소를 지원하지 않는 대기 핸들의 차단을 해제하는 방법을 보여 줍니다.
' Imports System
' Imports System.Threading
' Imports System.Threading.Tasks
Class CancelOldStyleEvents
' Old-style MRE that doesn't support unified cancellation.
Shared mre As New ManualResetEvent(False)
Shared Sub Main()
Dim cts As New CancellationTokenSource()
' Pass the same token source to the delegate and to the task instance.
Task.Factory.StartNew(Sub() DoWork(cts.Token), cts.Token)
Console.WriteLine("Press c to cancel, p to pause, or s to start/restart.")
Console.WriteLine("Or any other key to exit.")
' Old-style UI thread.
Dim goAgain As Boolean = True
While goAgain = True
Dim ch As Char = Console.ReadKey().KeyChar
Select Case ch
Case "c"c
cts.Cancel()
Case "p"c
mre.Reset()
Case "s"c
mre.Set()
Case Else
goAgain = False
End Select
Thread.Sleep(100)
End While
End Sub
Shared Sub DoWork(ByVal token As CancellationToken)
While True
' Wait on the event if it is not signaled.
Dim myWaitHandle(2) As WaitHandle
myWaitHandle(0) = mre
myWaitHandle(1) = token.WaitHandle
Dim eventThatSignaledIndex =
WaitHandle.WaitAny(myWaitHandle, _
New TimeSpan(0, 0, 20))
' Were we canceled while waiting?
' The first If statement is equivalent to
' token.ThrowIfCancellationRequested()
If eventThatSignaledIndex = 1 Then
Console.WriteLine("The wait operation was canceled.")
Throw New OperationCanceledException(token)
' Were we canceled while running?
ElseIf token.IsCancellationRequested = True Then
Console.WriteLine("Cancelling per user request.")
token.ThrowIfCancellationRequested()
' Did we time out?
ElseIf eventThatSignaledIndex = WaitHandle.WaitTimeout Then
Console.WriteLine("The wait operation timed out.")
Exit While
Else
' Simulating work.
Console.Write("Working... ")
Thread.SpinWait(5000000)
End If
End While
End Sub
End Class
using System;
using System.Threading;
using System.Threading.Tasks;
class CancelOldStyleEvents
{
// Old-style MRE that doesn't support unified cancellation.
static ManualResetEvent mre = new ManualResetEvent(false);
static void Main()
{
var cts = new CancellationTokenSource();
// Pass the same token source to the delegate and to the task instance.
Task.Factory.StartNew(() => DoWork(cts.Token), cts.Token);
Console.WriteLine("Press s to start/restart, p to pause, or c to cancel.");
Console.WriteLine("Or any other key to exit.");
// Old-style UI thread.
bool goAgain = true;
while (goAgain)
{
char ch = Console.ReadKey().KeyChar;
switch (ch)
{
case 'c':
cts.Cancel();
break;
case 'p':
mre.Reset();
break;
case 's':
mre.Set();
break;
default:
goAgain = false;
break;
}
Thread.Sleep(100);
}
}
static void DoWork(CancellationToken token)
{
while (true)
{
// Wait on the event if it is not signaled.
int eventThatSignaledIndex =
WaitHandle.WaitAny(new WaitHandle[] { mre, token.WaitHandle },
new TimeSpan(0, 0, 20));
// Were we canceled while waiting?
if (eventThatSignaledIndex == 1)
{
Console.WriteLine("The wait operation was canceled.");
throw new OperationCanceledException(token);
}
// Were we canceled while running?
else if (token.IsCancellationRequested)
{
Console.WriteLine("I was canceled while running.");
token.ThrowIfCancellationRequested();
}
// Did we time out?
else if (eventThatSignaledIndex == WaitHandle.WaitTimeout)
{
Console.WriteLine("I timed out.");
break;
}
else
{
Console.Write("Working... ");
// Simulating work.
Thread.SpinWait(5000000);
}
}
}
}
다음 예제에서는 ManualResetEventSlim을 사용하여 통합 취소를 지원하지 않는 코디네이션 기본 형식의 차단을 해제하는 방법을 보여 줍니다. 이 방법은 SemaphoreSlim 및 CountdownEvent 같은 다른 간단한 코디네이션 기본 형식에도 사용할 수 있습니다.
Class CancelNewStyleEvents
' New-style MRESlim that supports unified cancellation
' in its Wait methods.
Shared mres As ManualResetEventSlim = New ManualResetEventSlim(False)
Shared Sub Main()
Dim cts As New CancellationTokenSource()
' Pass the same token source to the delegate and to the task instance.
Task.Factory.StartNew(Sub() DoWork(cts.Token), cts.Token)
Console.WriteLine("Press c to cancel, p to pause, or s to start/restart,")
Console.WriteLine("or any other key to exit.")
' New-style UI thread.
Dim goAgain As Boolean = True
While goAgain = True
Dim ch As Char = Console.ReadKey().KeyChar
Select Case ch
Case "c"c
' Token can only be canceled once.
cts.Cancel()
Case "p"c
mres.Reset()
Case "s"c
mres.Set()
Case Else
goAgain = False
End Select
Thread.Sleep(100)
End While
End Sub
Shared Sub DoWork(ByVal token As CancellationToken)
While True
If token.IsCancellationRequested Then
Console.WriteLine("Canceled while running.")
token.ThrowIfCancellationRequested()
End If
' Wait on the event to be signaled
' or the token to be canceled,
' whichever comes first. The token
' will throw an exception if it is canceled
' while the thread is waiting on the event.
Try
' mres is a ManualResetEventSlim
mres.Wait(token)
Catch e As OperationCanceledException
' Throw immediately to be responsive. The
' alternative is to do one more item of work,
' and throw on next iteration, because
' IsCancellationRequested will be true.
Console.WriteLine("Canceled while waiting.")
Throw
End Try
' Simulating work.
Console.Write("Working...")
Thread.SpinWait(500000)
End While
End Sub
End Class
class CancelNewStyleEvents
{
// New-style MRESlim that supports unified cancellation
// in its Wait methods.
static ManualResetEventSlim mres = new ManualResetEventSlim(false);
static void Main()
{
var cts = new CancellationTokenSource();
// Pass the same token source to the delegate and to the task instance.
Task.Factory.StartNew(() => DoWork(cts.Token), cts.Token);
Console.WriteLine("Press c to cancel, p to pause, or s to start/restart,");
Console.WriteLine("or any other key to exit.");
// New-style UI thread.
bool goAgain = true;
while (goAgain)
{
char ch = Console.ReadKey().KeyChar;
switch (ch)
{
case 'c':
// Token can only be canceled once.
cts.Cancel();
break;
case 'p':
mres.Reset();
break;
case 's':
mres.Set();
break;
default:
goAgain = false;
break;
}
Thread.Sleep(100);
}
}
static void DoWork(CancellationToken token)
{
while (true)
{
if (token.IsCancellationRequested)
{
Console.WriteLine("Canceled while running.");
token.ThrowIfCancellationRequested();
}
// Wait on the event to be signaled
// or the token to be canceled,
// whichever comes first. The token
// will throw an exception if it is canceled
// while the thread is waiting on the event.
try
{
// mres is a ManualResetEventSlim
mres.Wait(token);
}
catch (OperationCanceledException)
{
// Throw immediately to be responsive. The
// alternative is to do one more item of work,
// and throw on next iteration, because
// IsCancellationRequested will be true.
Console.WriteLine("The wait operation was canceled.");
throw;
}
Console.Write("Working...");
// Simulating work.
Thread.SpinWait(500000);
}
}
}