WaitHandle.SignalAndWait 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
신호를 받고 WaitHandle 다른 신호를 기다립니다.
오버로드
| Name | Description |
|---|---|
| SignalAndWait(WaitHandle, WaitHandle) |
신호를 받고 WaitHandle 다른 신호를 기다립니다. |
| SignalAndWait(WaitHandle, WaitHandle, Int32, Boolean) |
대기 WaitHandle 를 입력하기 전에 시간 제한 간격을 32비트 부호 있는 정수로 지정하고 컨텍스트에 대한 동기화 도메인을 종료할지 여부를 지정하여 신호를 받고 다른 신호를 대기합니다. |
| SignalAndWait(WaitHandle, WaitHandle, TimeSpan, Boolean) |
대기를 입력하기 전에 시간 WaitHandle 제한 간격을 a TimeSpan 로 지정하고 컨텍스트에 대한 동기화 도메인을 종료할지 여부를 지정하여 신호를 받고 다른 신호를 대기합니다. |
SignalAndWait(WaitHandle, WaitHandle)
신호를 받고 WaitHandle 다른 신호를 기다립니다.
public:
static bool SignalAndWait(System::Threading::WaitHandle ^ toSignal, System::Threading::WaitHandle ^ toWaitOn);
public static bool SignalAndWait(System.Threading.WaitHandle toSignal, System.Threading.WaitHandle toWaitOn);
static member SignalAndWait : System.Threading.WaitHandle * System.Threading.WaitHandle -> bool
Public Shared Function SignalAndWait (toSignal As WaitHandle, toWaitOn As WaitHandle) As Boolean
매개 변수
- toSignal
- WaitHandle
WaitHandle 신호입니다.
- toWaitOn
- WaitHandle
WaitHandle 기다릴 것입니다.
반품
true 신호와 대기가 모두 성공적으로 완료되면 이고, 대기가 완료되지 않으면 메서드가 반환되지 않습니다.
예외
메서드가 상태의 스레드에서 호출되었습니다 STA .
toSignal 는 세마포이며 이미 전체 개수가 있습니다.
스레드가 뮤텍스를 해제하지 않고 종료되었기 때문에 대기가 완료되었습니다.
예제
다음 코드 예제에서는 메서드 오버로드를 사용하여 SignalAndWait(WaitHandle, WaitHandle) 주 스레드가 차단된 스레드에 신호를 보낼 수 있도록 한 다음 스레드가 작업을 완료할 때까지 기다립니다.
이 예제에서는 5개의 스레드를 시작하고, 플래그를 사용하여 만든 EventResetMode.AutoReset 스레드를 EventWaitHandle 차단한 다음, 사용자가 ENTER 키를 누를 때마다 하나의 스레드를 해제합니다. 그런 다음 이 예제에서는 다른 5개의 스레드를 큐에 대기하고 플래그와 함께 EventWaitHandle 만든 스레드를 EventResetMode.ManualReset 사용하여 모두 해제합니다.
using System;
using System.Threading;
public class Example
{
// The EventWaitHandle used to demonstrate the difference
// between AutoReset and ManualReset synchronization events.
//
private static EventWaitHandle ewh;
// A counter to make sure all threads are started and
// blocked before any are released. A Long is used to show
// the use of the 64-bit Interlocked methods.
//
private static long threadCount = 0;
// An AutoReset event that allows the main thread to block
// until an exiting thread has decremented the count.
//
private static EventWaitHandle clearCount =
new EventWaitHandle(false, EventResetMode.AutoReset);
[MTAThread]
public static void Main()
{
// Create an AutoReset EventWaitHandle.
//
ewh = new EventWaitHandle(false, EventResetMode.AutoReset);
// Create and start five numbered threads. Use the
// ParameterizedThreadStart delegate, so the thread
// number can be passed as an argument to the Start
// method.
for (int i = 0; i <= 4; i++)
{
Thread t = new Thread(
new ParameterizedThreadStart(ThreadProc)
);
t.Start(i);
}
// Wait until all the threads have started and blocked.
// When multiple threads use a 64-bit value on a 32-bit
// system, you must access the value through the
// Interlocked class to guarantee thread safety.
//
while (Interlocked.Read(ref threadCount) < 5)
{
Thread.Sleep(500);
}
// Release one thread each time the user presses ENTER,
// until all threads have been released.
//
while (Interlocked.Read(ref threadCount) > 0)
{
Console.WriteLine("Press ENTER to release a waiting thread.");
Console.ReadLine();
// SignalAndWait signals the EventWaitHandle, which
// releases exactly one thread before resetting,
// because it was created with AutoReset mode.
// SignalAndWait then blocks on clearCount, to
// allow the signaled thread to decrement the count
// before looping again.
//
WaitHandle.SignalAndWait(ewh, clearCount);
}
Console.WriteLine();
// Create a ManualReset EventWaitHandle.
//
ewh = new EventWaitHandle(false, EventResetMode.ManualReset);
// Create and start five more numbered threads.
//
for(int i=0; i<=4; i++)
{
Thread t = new Thread(
new ParameterizedThreadStart(ThreadProc)
);
t.Start(i);
}
// Wait until all the threads have started and blocked.
//
while (Interlocked.Read(ref threadCount) < 5)
{
Thread.Sleep(500);
}
// Because the EventWaitHandle was created with
// ManualReset mode, signaling it releases all the
// waiting threads.
//
Console.WriteLine("Press ENTER to release the waiting threads.");
Console.ReadLine();
ewh.Set();
}
public static void ThreadProc(object data)
{
int index = (int) data;
Console.WriteLine("Thread {0} blocks.", data);
// Increment the count of blocked threads.
Interlocked.Increment(ref threadCount);
// Wait on the EventWaitHandle.
ewh.WaitOne();
Console.WriteLine("Thread {0} exits.", data);
// Decrement the count of blocked threads.
Interlocked.Decrement(ref threadCount);
// After signaling ewh, the main thread blocks on
// clearCount until the signaled thread has
// decremented the count. Signal it now.
//
clearCount.Set();
}
}
Imports System.Threading
Public Class Example
' The EventWaitHandle used to demonstrate the difference
' between AutoReset and ManualReset synchronization events.
'
Private Shared ewh As EventWaitHandle
' A counter to make sure all threads are started and
' blocked before any are released. A Long is used to show
' the use of the 64-bit Interlocked methods.
'
Private Shared threadCount As Long = 0
' An AutoReset event that allows the main thread to block
' until an exiting thread has decremented the count.
'
Private Shared clearCount As New EventWaitHandle(False, _
EventResetMode.AutoReset)
<MTAThread> _
Public Shared Sub Main()
' Create an AutoReset EventWaitHandle.
'
ewh = New EventWaitHandle(False, EventResetMode.AutoReset)
' Create and start five numbered threads. Use the
' ParameterizedThreadStart delegate, so the thread
' number can be passed as an argument to the Start
' method.
For i As Integer = 0 To 4
Dim t As New Thread(AddressOf ThreadProc)
t.Start(i)
Next i
' Wait until all the threads have started and blocked.
' When multiple threads use a 64-bit value on a 32-bit
' system, you must access the value through the
' Interlocked class to guarantee thread safety.
'
While Interlocked.Read(threadCount) < 5
Thread.Sleep(500)
End While
' Release one thread each time the user presses ENTER,
' until all threads have been released.
'
While Interlocked.Read(threadCount) > 0
Console.WriteLine("Press ENTER to release a waiting thread.")
Console.ReadLine()
' SignalAndWait signals the EventWaitHandle, which
' releases exactly one thread before resetting,
' because it was created with AutoReset mode.
' SignalAndWait then blocks on clearCount, to
' allow the signaled thread to decrement the count
' before looping again.
'
WaitHandle.SignalAndWait(ewh, clearCount)
End While
Console.WriteLine()
' Create a ManualReset EventWaitHandle.
'
ewh = New EventWaitHandle(False, EventResetMode.ManualReset)
' Create and start five more numbered threads.
'
For i As Integer = 0 To 4
Dim t As New Thread(AddressOf ThreadProc)
t.Start(i)
Next i
' Wait until all the threads have started and blocked.
'
While Interlocked.Read(threadCount) < 5
Thread.Sleep(500)
End While
' Because the EventWaitHandle was created with
' ManualReset mode, signaling it releases all the
' waiting threads.
'
Console.WriteLine("Press ENTER to release the waiting threads.")
Console.ReadLine()
ewh.Set()
End Sub
Public Shared Sub ThreadProc(ByVal data As Object)
Dim index As Integer = CInt(data)
Console.WriteLine("Thread {0} blocks.", data)
' Increment the count of blocked threads.
Interlocked.Increment(threadCount)
' Wait on the EventWaitHandle.
ewh.WaitOne()
Console.WriteLine("Thread {0} exits.", data)
' Decrement the count of blocked threads.
Interlocked.Decrement(threadCount)
' After signaling ewh, the main thread blocks on
' clearCount until the signaled thread has
' decremented the count. Signal it now.
'
clearCount.Set()
End Sub
End Class
설명
이 작업은 원자성으로 보장되지 않습니다. 현재 스레드가 신호를 받은 toSignal 후 대기하기 전에 다른 프로세서에서 toWaitOn실행 중인 스레드가 신호를 toWaitOn 표시하거나 대기할 수 있습니다.
적용 대상
SignalAndWait(WaitHandle, WaitHandle, Int32, Boolean)
대기 WaitHandle 를 입력하기 전에 시간 제한 간격을 32비트 부호 있는 정수로 지정하고 컨텍스트에 대한 동기화 도메인을 종료할지 여부를 지정하여 신호를 받고 다른 신호를 대기합니다.
public:
static bool SignalAndWait(System::Threading::WaitHandle ^ toSignal, System::Threading::WaitHandle ^ toWaitOn, int millisecondsTimeout, bool exitContext);
public static bool SignalAndWait(System.Threading.WaitHandle toSignal, System.Threading.WaitHandle toWaitOn, int millisecondsTimeout, bool exitContext);
static member SignalAndWait : System.Threading.WaitHandle * System.Threading.WaitHandle * int * bool -> bool
Public Shared Function SignalAndWait (toSignal As WaitHandle, toWaitOn As WaitHandle, millisecondsTimeout As Integer, exitContext As Boolean) As Boolean
매개 변수
- toSignal
- WaitHandle
WaitHandle 신호입니다.
- toWaitOn
- WaitHandle
WaitHandle 기다릴 것입니다.
- exitContext
- Boolean
true대기 전에 컨텍스트에 대한 동기화 도메인을 종료하고(동기화된 컨텍스트에 있는 경우) 나중에 다시 가져옵니다. 그렇지 않으면 . false
반품
true 신호와 대기가 모두 성공적으로 false 완료되거나 신호가 완료되었지만 대기 시간이 초과된 경우
예외
이 메서드는 상태의 스레드에서 호출됩니다 STA .
최대 WaitHandle 개수를 초과하므로 신호를 받을 수 없습니다.
millisecondsTimeout 는 무한 제한 시간을 나타내는 -1 이외의 음수입니다.
스레드가 뮤텍스를 해제하지 않고 종료되었기 때문에 대기가 완료되었습니다.
설명
이 작업은 원자성으로 보장되지 않습니다. 현재 스레드가 신호를 받은 toSignal 후 대기하기 전에 다른 프로세서에서 toWaitOn실행 중인 스레드가 신호를 toWaitOn 표시하거나 대기할 수 있습니다.
0이면 millisecondsTimeout 메서드가 차단되지 않습니다. 상태를 toWaitOn 테스트하고 즉시 반환합니다.
컨텍스트 종료
이 exitContext 메서드가 기본이 아닌 관리되는 컨텍스트 내에서 호출되지 않는 한 매개 변수는 효과가 없습니다. 스레드가 파생된 클래스의 인스턴스에 대한 호출 내에 있는 경우 관리되는 ContextBoundObject컨텍스트는 기본이 아닐 수 있습니다. 현재 파생되지 않은 ContextBoundObjectString클래스에서 메서드를 실행하는 경우에도 현재 애플리케이션 도메인의 스택에 있는 경우 ContextBoundObject 기본이 아닌 컨텍스트에 있을 수 있습니다.
코드가 기본이 아닌 컨텍스트에서 실행되는 경우 이 메서드를 trueexitContext 실행하기 전에 스레드가 기본 컨텍스트로 전환하기 위해 비디폴트 관리되는 컨텍스트(즉, 기본 컨텍스트로 전환)를 종료하도록 지정합니다. 스레드는 이 메서드에 대한 호출이 완료된 후 원래의 기본이 아닌 컨텍스트로 돌아갑니다.
컨텍스트를 종료하는 것은 컨텍스트 바인딩 클래스에 특성이 SynchronizationAttribute 있는 경우에 유용할 수 있습니다. 이 경우 클래스의 멤버에 대한 모든 호출이 자동으로 동기화되고 동기화 도메인은 클래스의 전체 코드 본문입니다. 멤버의 호출 스택에 있는 코드가 이 메서드를 true호출하고 이를 지정 exitContext 하는 경우 스레드는 동기화 도메인을 종료합니다. 그러면 개체의 멤버를 호출할 때 차단된 스레드를 계속 진행할 수 있습니다. 이 메서드가 반환되면 호출한 스레드가 동기화 도메인을 다시 입력하기 위해 기다려야 합니다.
적용 대상
SignalAndWait(WaitHandle, WaitHandle, TimeSpan, Boolean)
대기를 입력하기 전에 시간 WaitHandle 제한 간격을 a TimeSpan 로 지정하고 컨텍스트에 대한 동기화 도메인을 종료할지 여부를 지정하여 신호를 받고 다른 신호를 대기합니다.
public:
static bool SignalAndWait(System::Threading::WaitHandle ^ toSignal, System::Threading::WaitHandle ^ toWaitOn, TimeSpan timeout, bool exitContext);
public static bool SignalAndWait(System.Threading.WaitHandle toSignal, System.Threading.WaitHandle toWaitOn, TimeSpan timeout, bool exitContext);
static member SignalAndWait : System.Threading.WaitHandle * System.Threading.WaitHandle * TimeSpan * bool -> bool
Public Shared Function SignalAndWait (toSignal As WaitHandle, toWaitOn As WaitHandle, timeout As TimeSpan, exitContext As Boolean) As Boolean
매개 변수
- toSignal
- WaitHandle
WaitHandle 신호입니다.
- toWaitOn
- WaitHandle
WaitHandle 기다릴 것입니다.
- exitContext
- Boolean
true대기 전에 컨텍스트에 대한 동기화 도메인을 종료하고(동기화된 컨텍스트에 있는 경우) 나중에 다시 가져옵니다. 그렇지 않으면 . false
반품
true 신호와 대기가 모두 성공적으로 false 완료되거나 신호가 완료되었지만 대기 시간이 초과된 경우
예외
메서드가 상태의 스레드에서 호출되었습니다 STA .
toSignal 는 세마포이며 이미 전체 개수가 있습니다.
스레드가 뮤텍스를 해제하지 않고 종료되었기 때문에 대기가 완료되었습니다.
설명
이 작업은 원자성으로 보장되지 않습니다. 현재 스레드가 신호를 받은 toSignal 후 대기하기 전에 다른 프로세서에서 toWaitOn실행 중인 스레드가 신호를 toWaitOn 표시하거나 대기할 수 있습니다.
최대값 timeout 은 .입니다 Int32.MaxValue.
0이면 timeout 메서드가 차단되지 않습니다. 상태를 toWaitOn 테스트하고 즉시 반환합니다.
컨텍스트 종료
이 exitContext 메서드가 기본이 아닌 관리되는 컨텍스트 내에서 호출되지 않는 한 매개 변수는 효과가 없습니다. 스레드가 파생된 클래스의 인스턴스에 대한 호출 내에 있는 경우 관리되는 ContextBoundObject컨텍스트는 기본이 아닐 수 있습니다. 현재 파생되지 않은 ContextBoundObjectString클래스에서 메서드를 실행하는 경우에도 현재 애플리케이션 도메인의 스택에 있는 경우 ContextBoundObject 기본이 아닌 컨텍스트에 있을 수 있습니다.
코드가 기본이 아닌 컨텍스트에서 실행되는 경우 이 메서드를 trueexitContext 실행하기 전에 스레드가 기본 컨텍스트로 전환하기 위해 비디폴트 관리되는 컨텍스트(즉, 기본 컨텍스트로 전환)를 종료하도록 지정합니다. 스레드는 이 메서드에 대한 호출이 완료된 후 원래의 기본이 아닌 컨텍스트로 돌아갑니다.
컨텍스트를 종료하는 것은 컨텍스트 바인딩 클래스에 특성이 SynchronizationAttribute 있는 경우에 유용할 수 있습니다. 이 경우 클래스의 멤버에 대한 모든 호출이 자동으로 동기화되고 동기화 도메인은 클래스의 전체 코드 본문입니다. 멤버의 호출 스택에 있는 코드가 이 메서드를 true호출하고 이를 지정 exitContext 하는 경우 스레드는 동기화 도메인을 종료합니다. 그러면 개체의 멤버를 호출할 때 차단된 스레드를 계속 진행할 수 있습니다. 이 메서드가 반환되면 호출한 스레드가 동기화 도메인을 다시 입력하기 위해 기다려야 합니다.