ThreadPool.RegisterWaitForSingleObject 方法
定义
注册正在等待 WaitHandle 的委托。Registers a delegate that is waiting for a WaitHandle.
重载
RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, UInt32, Boolean) |
指定表示超时(以毫秒为单位)的 32 位无符号整数,注册一个委托等待 WaitHandle。Registers a delegate to wait for a WaitHandle, specifying a 32-bit unsigned integer for the time-out in milliseconds. |
RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, TimeSpan, Boolean) |
注册一个等待 WaitHandle 的委托,并指定一个 TimeSpan 值来表示超时时间。Registers a delegate to wait for a WaitHandle, specifying a TimeSpan value for the time-out. |
RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, Int32, Boolean) |
注册一个等待 WaitHandle 的委托,并指定一个 32 位有符号整数来表示超时值(以毫秒为单位)。Registers a delegate to wait for a WaitHandle, specifying a 32-bit signed integer for the time-out in milliseconds. |
RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, Int64, Boolean) |
注册一个等待 WaitHandle 的委托,并指定一个 64 位有符号整数来表示超时值(以毫秒为单位)。Registers a delegate to wait for a WaitHandle, specifying a 64-bit signed integer for the time-out in milliseconds. |
RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, UInt32, Boolean)
重要
此 API 不符合 CLS。
指定表示超时(以毫秒为单位)的 32 位无符号整数,注册一个委托等待 WaitHandle。Registers a delegate to wait for a WaitHandle, specifying a 32-bit unsigned integer for the time-out in milliseconds.
public:
static System::Threading::RegisteredWaitHandle ^ RegisterWaitForSingleObject(System::Threading::WaitHandle ^ waitObject, System::Threading::WaitOrTimerCallback ^ callBack, System::Object ^ state, System::UInt32 millisecondsTimeOutInterval, bool executeOnlyOnce);
[System.CLSCompliant(false)]
public static System.Threading.RegisteredWaitHandle RegisterWaitForSingleObject (System.Threading.WaitHandle waitObject, System.Threading.WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval, bool executeOnlyOnce);
static member RegisterWaitForSingleObject : System.Threading.WaitHandle * System.Threading.WaitOrTimerCallback * obj * uint32 * bool -> System.Threading.RegisteredWaitHandle
Public Shared Function RegisterWaitForSingleObject (waitObject As WaitHandle, callBack As WaitOrTimerCallback, state As Object, millisecondsTimeOutInterval As UInteger, executeOnlyOnce As Boolean) As RegisteredWaitHandle
参数
- waitObject
- WaitHandle
要注册的 WaitHandle。The WaitHandle to register. 使用 WaitHandle 而非 Mutex。Use a WaitHandle other than Mutex.
- callBack
- WaitOrTimerCallback
向 waitObject
参数发出信号时调用的 WaitOrTimerCallback 委托。The WaitOrTimerCallback delegate to call when the waitObject
parameter is signaled.
- state
- Object
传递给委托的对象。The object passed to the delegate.
- millisecondsTimeOutInterval
- UInt32
以毫秒为单位的超时。The time-out in milliseconds. 如果 millisecondsTimeOutInterval
参数为 0(零),函数将测试对象的状态并立即返回。If the millisecondsTimeOutInterval
parameter is 0 (zero), the function tests the object's state and returns immediately. 如果 millisecondsTimeOutInterval
为 -1,则函数的超时间隔永远不过期。If millisecondsTimeOutInterval
is -1, the function's time-out interval never elapses.
- executeOnlyOnce
- Boolean
如果为 true
,表示在调用了委托后,线程将不再在 waitObject
参数上等待;如果为 false
,表示每次完成等待操作后都重置计时器,直到注销等待。true
to indicate that the thread will no longer wait on the waitObject
parameter after the delegate has been called; false
to indicate that the timer is reset every time the wait operation completes until the wait is unregistered.
返回
RegisteredWaitHandle,可用于取消已注册的等待操作。The RegisteredWaitHandle that can be used to cancel the registered wait operation.
- 属性
异常
millisecondsTimeOutInterval
参数小于 -1。The millisecondsTimeOutInterval
parameter is less than -1.
示例
下面的示例演示如何使用 RegisterWaitForSingleObject 方法在指定的等待句柄终止时执行指定的回调方法。The following example shows how to use the RegisterWaitForSingleObject method to execute a specified callback method when a specified wait handle is signaled. 在此示例中,回调方法为 WaitProc
,等待句柄为 AutoResetEvent。In this example, the callback method is WaitProc
, and the wait handle is an AutoResetEvent.
该示例定义了一个 @no__t 0 类,用于保存在执行时传递给回调的信息。The example defines a TaskInfo
class to hold the information that is passed to the callback when it executes. 该示例创建一个 @no__t 0 对象,并为其分配一些字符串数据。The example creates a TaskInfo
object and assigns it some string data. @No__t-1 方法返回的 @no__t 是分配给该 @no__t 对象的 Handle
字段,以便回调方法有权访问 @no__t。The RegisteredWaitHandle that is returned by the RegisterWaitForSingleObject method is assigned to the Handle
field of the TaskInfo
object so that the callback method has access to the RegisteredWaitHandle.
除了指定 TaskInfo
作为要传递给回调方法的对象之外,对 RegisterWaitForSingleObject 方法的调用还指定任务将等待的 AutoResetEvent @no__t,该委托表示 @no__t 的回调方法,一秒的超时间隔和多个回调。In addition to specifying TaskInfo
as the object to pass to the callback method, the call to the RegisterWaitForSingleObject method specifies the AutoResetEvent that the task will wait for, a WaitOrTimerCallback delegate that represents the WaitProc
callback method, a one second time-out interval, and multiple callbacks.
当主线程通过调用其 @no__t 的方法发出 @no__t 的信号时,将调用 WaitOrTimerCallback 委托。When the main thread signals the AutoResetEvent by calling its Set method, the WaitOrTimerCallback delegate is invoked. @No__t-0 方法测试 RegisteredWaitHandle,以确定是否发生了超时。The WaitProc
method tests RegisteredWaitHandle to determine whether a time-out occurred. 如果回调是由于等待句柄已发出信号而调用的,则 WaitProc
方法会取消 RegisteredWaitHandle 的注册,从而停止其他回调。If the callback was invoked because the wait handle was signaled, the WaitProc
method unregisters the RegisteredWaitHandle, stopping additional callbacks. 在超时情况下,任务将继续等待。In the case of a time-out, the task continues to wait. @No__t-0 方法通过将消息打印到控制台来结束。The WaitProc
method ends by printing a message to the console.
using namespace System;
using namespace System::Threading;
// TaskInfo contains data that will be passed to the callback
// method.
public ref class TaskInfo
{
public:
TaskInfo()
{
Handle = nullptr;
OtherInfo = "default";
}
RegisteredWaitHandle^ Handle;
String^ OtherInfo;
};
ref class Example
{
public:
// The callback method executes when the registered wait times out,
// or when the WaitHandle (in this case AutoResetEvent) is signaled.
// WaitProc unregisters the WaitHandle the first time the event is
// signaled.
static void WaitProc( Object^ state, bool timedOut )
{
// The state Object must be cast to the correct type, because the
// signature of the WaitOrTimerCallback delegate specifies type
// Object.
TaskInfo^ ti = static_cast<TaskInfo^>(state);
String^ cause = "TIMED OUT";
if ( !timedOut )
{
cause = "SIGNALED";
// If the callback method executes because the WaitHandle is
// signaled, stop future execution of the callback method
// by unregistering the WaitHandle.
if ( ti->Handle != nullptr )
ti->Handle->Unregister( nullptr );
}
Console::WriteLine( "WaitProc( {0}) executes on thread {1}; cause = {2}.", ti->OtherInfo, Thread::CurrentThread->GetHashCode(), cause );
}
};
int main()
{
// The main thread uses AutoResetEvent to signal the
// registered wait handle, which executes the callback
// method.
AutoResetEvent^ ev = gcnew AutoResetEvent( false );
TaskInfo^ ti = gcnew TaskInfo;
ti->OtherInfo = "First task";
// The TaskInfo for the task includes the registered wait
// handle returned by RegisterWaitForSingleObject. This
// allows the wait to be terminated when the object has
// been signaled once (see WaitProc).
ti->Handle = ThreadPool::RegisterWaitForSingleObject( ev, gcnew WaitOrTimerCallback( Example::WaitProc ), ti, 1000, false );
// The main thread waits three seconds, to demonstrate the
// time-outs on the queued thread, and then signals.
Thread::Sleep( 3100 );
Console::WriteLine( "Main thread signals." );
ev->Set();
// The main thread sleeps, which should give the callback
// method time to execute. If you comment out this line, the
// program usually ends before the ThreadPool thread can execute.
Thread::Sleep( 1000 );
// If you start a thread yourself, you can wait for it to end
// by calling Thread::Join. This option is not available with
// thread pool threads.
return 0;
}
using System;
using System.Threading;
// TaskInfo contains data that will be passed to the callback
// method.
public class TaskInfo {
public RegisteredWaitHandle Handle = null;
public string OtherInfo = "default";
}
public class Example {
public static void Main(string[] args) {
// The main thread uses AutoResetEvent to signal the
// registered wait handle, which executes the callback
// method.
AutoResetEvent ev = new AutoResetEvent(false);
TaskInfo ti = new TaskInfo();
ti.OtherInfo = "First task";
// The TaskInfo for the task includes the registered wait
// handle returned by RegisterWaitForSingleObject. This
// allows the wait to be terminated when the object has
// been signaled once (see WaitProc).
ti.Handle = ThreadPool.RegisterWaitForSingleObject(
ev,
new WaitOrTimerCallback(WaitProc),
ti,
1000,
false
);
// The main thread waits three seconds, to demonstrate the
// time-outs on the queued thread, and then signals.
Thread.Sleep(3100);
Console.WriteLine("Main thread signals.");
ev.Set();
// The main thread sleeps, which should give the callback
// method time to execute. If you comment out this line, the
// program usually ends before the ThreadPool thread can execute.
Thread.Sleep(1000);
// If you start a thread yourself, you can wait for it to end
// by calling Thread.Join. This option is not available with
// thread pool threads.
}
// The callback method executes when the registered wait times out,
// or when the WaitHandle (in this case AutoResetEvent) is signaled.
// WaitProc unregisters the WaitHandle the first time the event is
// signaled.
public static void WaitProc(object state, bool timedOut) {
// The state object must be cast to the correct type, because the
// signature of the WaitOrTimerCallback delegate specifies type
// Object.
TaskInfo ti = (TaskInfo) state;
string cause = "TIMED OUT";
if (!timedOut) {
cause = "SIGNALED";
// If the callback method executes because the WaitHandle is
// signaled, stop future execution of the callback method
// by unregistering the WaitHandle.
if (ti.Handle != null)
ti.Handle.Unregister(null);
}
Console.WriteLine("WaitProc( {0} ) executes on thread {1}; cause = {2}.",
ti.OtherInfo,
Thread.CurrentThread.GetHashCode().ToString(),
cause
);
}
}
Imports System.Threading
' TaskInfo contains data that will be passed to the callback
' method.
Public Class TaskInfo
public Handle As RegisteredWaitHandle = Nothing
public OtherInfo As String = "default"
End Class
Public Class Example
<MTAThread> _
Public Shared Sub Main()
' The main thread uses AutoResetEvent to signal the
' registered wait handle, which executes the callback
' method.
Dim ev As New AutoResetEvent(false)
Dim ti As New TaskInfo()
ti.OtherInfo = "First task"
' The TaskInfo for the task includes the registered wait
' handle returned by RegisterWaitForSingleObject. This
' allows the wait to be terminated when the object has
' been signaled once (see WaitProc).
ti.Handle = ThreadPool.RegisterWaitForSingleObject( _
ev, _
New WaitOrTimerCallback(AddressOf WaitProc), _
ti, _
1000, _
false _
)
' The main thread waits about three seconds, to demonstrate
' the time-outs on the queued task, and then signals.
Thread.Sleep(3100)
Console.WriteLine("Main thread signals.")
ev.Set()
' The main thread sleeps, which should give the callback
' method time to execute. If you comment out this line, the
' program usually ends before the ThreadPool thread can execute.
Thread.Sleep(1000)
' If you start a thread yourself, you can wait for it to end
' by calling Thread.Join. This option is not available with
' thread pool threads.
End Sub
' The callback method executes when the registered wait times out,
' or when the WaitHandle (in this case AutoResetEvent) is signaled.
' WaitProc unregisters the WaitHandle the first time the event is
' signaled.
Public Shared Sub WaitProc(state As Object, timedOut As Boolean)
' The state object must be cast to the correct type, because the
' signature of the WaitOrTimerCallback delegate specifies type
' Object.
Dim ti As TaskInfo = CType(state, TaskInfo)
Dim cause As String = "TIMED OUT"
If Not timedOut Then
cause = "SIGNALED"
' If the callback method executes because the WaitHandle is
' signaled, stop future execution of the callback method
' by unregistering the WaitHandle.
If Not ti.Handle Is Nothing Then
ti.Handle.Unregister(Nothing)
End If
End If
Console.WriteLine("WaitProc( {0} ) executes on thread {1}; cause = {2}.", _
ti.OtherInfo, _
Thread.CurrentThread.GetHashCode().ToString(), _
cause _
)
End Sub
End Class
注解
使用完此方法返回的 RegisteredWaitHandle 后,调用其 @no__t 1 方法以释放对等待句柄的引用。When you are finished using the RegisteredWaitHandle that is returned by this method, call its RegisteredWaitHandle.Unregister method to release references to the wait handle. 建议始终调用 RegisteredWaitHandle.Unregister 方法,即使为 @no__t 指定 true
。We recommend that you always call the RegisteredWaitHandle.Unregister method, even if you specify true
for executeOnlyOnce
. 如果调用 RegisteredWaitHandle.Unregister 方法,而不是根据注册的等待句柄的终结器,则垃圾回收的工作效率更高。Garbage collection works more efficiently if you call the RegisteredWaitHandle.Unregister method instead of depending on the registered wait handle's finalizer.
@No__t-0 方法会将指定的委托排队到线程池。The RegisterWaitForSingleObject method queues the specified delegate to the thread pool. 当发生以下情况之一时,工作线程将执行委托:A worker thread will execute the delegate when one of the following occurs:
指定的对象处于终止状态。The specified object is in the signaled state.
超时间隔已过去。The time-out interval elapses.
@No__t-0 方法检查指定对象的 @no__t 的当前状态。The RegisterWaitForSingleObject method checks the current state of the specified object's WaitHandle. 如果对象的状态为信号,则该方法将注册等待操作。If the object's state is unsignaled, the method registers a wait operation. 等待操作由线程池中的一个线程执行。The wait operation is performed by a thread from the thread pool. 当对象的状态变为 "已终止" 或超时间隔结束时,工作线程会执行委托。The delegate is executed by a worker thread when the object's state becomes signaled or the time-out interval elapses. 如果 @no__t 参数不为0(零),并且 @no__t 参数 @no__t 为-2,则每当事件发出信号或超时间隔结束时,将重置计时器。If the timeOutInterval
parameter is not 0 (zero) and the executeOnlyOnce
parameter is false
, the timer is reset every time the event is signaled or the time-out interval elapses.
重要
使用 @no__t 为-1 的 @no__t 不会为回调提供互斥,因为基础 Windows API 使用默认的 WT_EXECUTEDEFAULT
标志,因此每个回调都在单独的线程池线程上进行调度。Using a Mutex for waitObject
does not provide mutual exclusion for the callbacks because the underlying Windows API uses the default WT_EXECUTEDEFAULT
flag, so each callback is dispatched on a separate thread pool thread. 请使用最大计数为1的 Semaphore,而不是 Mutex。Instead of a Mutex, use a Semaphore with a maximum count of 1.
若要取消等待操作,请调用 RegisteredWaitHandle.Unregister 方法。To cancel the wait operation, call the RegisteredWaitHandle.Unregister method.
等待线程使用 Win32 WaitForMultipleObjects
函数来监视已注册的等待操作。The wait thread uses the Win32 WaitForMultipleObjects
function to monitor registered wait operations. 因此,如果必须在多个调用中使用相同的本机操作系统句柄 RegisterWaitForSingleObject,则必须使用 Win32 DuplicateHandle
函数复制句柄。Therefore, if you must use the same native operating system handle in multiple calls to RegisterWaitForSingleObject, you must duplicate the handle using the Win32 DuplicateHandle
function. 请注意,不应脉冲传递到 RegisterWaitForSingleObject 的事件对象,因为等待线程可能检测不到事件在重置前已发出信号。Note that you should not pulse an event object passed to RegisterWaitForSingleObject, because the wait thread might not detect that the event is signaled before it is reset.
在返回之前,函数会修改某些类型的同步对象的状态。Before returning, the function modifies the state of some types of synchronization objects. 仅对其终止状态导致等待条件得到满足的对象进行修改。Modification occurs only for the object whose signaled state caused the wait condition to be satisfied. 例如,信号量的计数减少1。For example, the count of a semaphore is decreased by one.
版本信息Version Information
从 .NET Framework 版本2.0 开始,@no__t 属性值将传播到使用 @no__t 1 方法排队的工作线程。Starting with the .NET Framework version 2.0, the Thread.CurrentPrincipal property value is propagated to worker threads queued using the RegisterWaitForSingleObject method. 在早期版本中,不传播主体信息。In earlier versions, the principal information is not propagated.
另请参阅
RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, TimeSpan, Boolean)
注册一个等待 WaitHandle 的委托,并指定一个 TimeSpan 值来表示超时时间。Registers a delegate to wait for a WaitHandle, specifying a TimeSpan value for the time-out.
public:
static System::Threading::RegisteredWaitHandle ^ RegisterWaitForSingleObject(System::Threading::WaitHandle ^ waitObject, System::Threading::WaitOrTimerCallback ^ callBack, System::Object ^ state, TimeSpan timeout, bool executeOnlyOnce);
public static System.Threading.RegisteredWaitHandle RegisterWaitForSingleObject (System.Threading.WaitHandle waitObject, System.Threading.WaitOrTimerCallback callBack, object state, TimeSpan timeout, bool executeOnlyOnce);
static member RegisterWaitForSingleObject : System.Threading.WaitHandle * System.Threading.WaitOrTimerCallback * obj * TimeSpan * bool -> System.Threading.RegisteredWaitHandle
Public Shared Function RegisterWaitForSingleObject (waitObject As WaitHandle, callBack As WaitOrTimerCallback, state As Object, timeout As TimeSpan, executeOnlyOnce As Boolean) As RegisteredWaitHandle
参数
- waitObject
- WaitHandle
要注册的 WaitHandle。The WaitHandle to register. 使用 WaitHandle 而非 Mutex。Use a WaitHandle other than Mutex.
- callBack
- WaitOrTimerCallback
向 waitObject
参数发出信号时调用的 WaitOrTimerCallback 委托。The WaitOrTimerCallback delegate to call when the waitObject
parameter is signaled.
- state
- Object
传递给委托的对象。The object passed to the delegate.
- timeout
- TimeSpan
TimeSpan 表示的超时时间。The time-out represented by a TimeSpan. 如果 timeout
为 0(零),则函数将测试对象的状态并立即返回。If timeout
is 0 (zero), the function tests the object's state and returns immediately. 如果 timeout
为 -1,则函数的超时间隔永远不过期。If timeout
is -1, the function's time-out interval never elapses.
- executeOnlyOnce
- Boolean
如果为 true
,表示在调用了委托后,线程将不再在 waitObject
参数上等待;如果为 false
,表示每次完成等待操作后都重置计时器,直到注销等待。true
to indicate that the thread will no longer wait on the waitObject
parameter after the delegate has been called; false
to indicate that the timer is reset every time the wait operation completes until the wait is unregistered.
返回
封装本机句柄的 RegisteredWaitHandle。The RegisteredWaitHandle that encapsulates the native handle.
异常
timeout
参数小于 -1。The timeout
parameter is less than -1.
注解
使用完此方法返回的 RegisteredWaitHandle 后,调用其 @no__t 1 方法以释放对等待句柄的引用。When you are finished using the RegisteredWaitHandle that is returned by this method, call its RegisteredWaitHandle.Unregister method to release references to the wait handle. 建议始终调用 RegisteredWaitHandle.Unregister 方法,即使为 @no__t 指定 true
。We recommend that you always call the RegisteredWaitHandle.Unregister method, even if you specify true
for executeOnlyOnce
. 如果调用 RegisteredWaitHandle.Unregister 方法,而不是根据注册的等待句柄的终结器,则垃圾回收的工作效率更高。Garbage collection works more efficiently if you call the RegisteredWaitHandle.Unregister method instead of depending on the registered wait handle's finalizer.
@No__t-0 方法会将指定的委托排队到线程池。The RegisterWaitForSingleObject method queues the specified delegate to the thread pool. 当发生以下情况之一时,工作线程将执行委托:A worker thread will execute the delegate when one of the following occurs:
指定的对象处于终止状态。The specified object is in the signaled state.
超时间隔已过去。The time-out interval elapses.
@No__t-0 方法检查指定对象的 @no__t 的当前状态。The RegisterWaitForSingleObject method checks the current state of the specified object's WaitHandle. 如果对象的状态为信号,则该方法将注册等待操作。If the object's state is unsignaled, the method registers a wait operation. 等待操作由线程池中的一个线程执行。The wait operation is performed by a thread from the thread pool. 当对象的状态变为 "已终止" 或超时间隔结束时,工作线程会执行委托。The delegate is executed by a worker thread when the object's state becomes signaled or the time-out interval elapses. 如果 @no__t 参数不为0(零),并且 @no__t 参数 @no__t 为-2,则每当事件发出信号或超时间隔结束时,将重置计时器。If the timeOutInterval
parameter is not 0 (zero) and the executeOnlyOnce
parameter is false
, the timer is reset every time the event is signaled or the time-out interval elapses.
重要
使用 @no__t 为-1 的 @no__t 不会为回调提供互斥,因为基础 Windows API 使用默认的 WT_EXECUTEDEFAULT
标志,因此每个回调都在单独的线程池线程上进行调度。Using a Mutex for waitObject
does not provide mutual exclusion for the callbacks because the underlying Windows API uses the default WT_EXECUTEDEFAULT
flag, so each callback is dispatched on a separate thread pool thread. 请使用最大计数为1的 Semaphore,而不是 Mutex。Instead of a Mutex, use a Semaphore with a maximum count of 1.
若要取消等待操作,请调用 RegisteredWaitHandle.Unregister 方法。To cancel the wait operation, call the RegisteredWaitHandle.Unregister method.
等待线程使用 Win32 WaitForMultipleObjects
函数来监视已注册的等待操作。The wait thread uses the Win32 WaitForMultipleObjects
function to monitor registered wait operations. 因此,如果必须在多个调用中使用相同的本机操作系统句柄 RegisterWaitForSingleObject,则必须使用 Win32 DuplicateHandle
函数复制句柄。Therefore, if you must use the same native operating system handle in multiple calls to RegisterWaitForSingleObject, you must duplicate the handle using the Win32 DuplicateHandle
function. 请注意,不应脉冲传递到 RegisterWaitForSingleObject 的事件对象,因为等待线程可能检测不到事件在重置前已发出信号。Note that you should not pulse an event object passed to RegisterWaitForSingleObject, because the wait thread might not detect that the event is signaled before it is reset.
在返回之前,函数会修改某些类型的同步对象的状态。Before returning, the function modifies the state of some types of synchronization objects. 仅对其终止状态导致等待条件得到满足的对象进行修改。Modification occurs only for the object whose signaled state caused the wait condition to be satisfied. 例如,信号量的计数减少1。For example, the count of a semaphore is decreased by one.
版本信息Version Information
从 .NET Framework 版本2.0 开始,@no__t 属性值将传播到使用 @no__t 1 方法排队的工作线程。Starting with the .NET Framework version 2.0, the Thread.CurrentPrincipal property value is propagated to worker threads queued using the RegisterWaitForSingleObject method. 在早期版本中,不传播主体信息。In earlier versions, the principal information is not propagated.
另请参阅
RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, Int32, Boolean)
注册一个等待 WaitHandle 的委托,并指定一个 32 位有符号整数来表示超时值(以毫秒为单位)。Registers a delegate to wait for a WaitHandle, specifying a 32-bit signed integer for the time-out in milliseconds.
public:
static System::Threading::RegisteredWaitHandle ^ RegisterWaitForSingleObject(System::Threading::WaitHandle ^ waitObject, System::Threading::WaitOrTimerCallback ^ callBack, System::Object ^ state, int millisecondsTimeOutInterval, bool executeOnlyOnce);
public static System.Threading.RegisteredWaitHandle RegisterWaitForSingleObject (System.Threading.WaitHandle waitObject, System.Threading.WaitOrTimerCallback callBack, object state, int millisecondsTimeOutInterval, bool executeOnlyOnce);
static member RegisterWaitForSingleObject : System.Threading.WaitHandle * System.Threading.WaitOrTimerCallback * obj * int * bool -> System.Threading.RegisteredWaitHandle
Public Shared Function RegisterWaitForSingleObject (waitObject As WaitHandle, callBack As WaitOrTimerCallback, state As Object, millisecondsTimeOutInterval As Integer, executeOnlyOnce As Boolean) As RegisteredWaitHandle
参数
- waitObject
- WaitHandle
要注册的 WaitHandle。The WaitHandle to register. 使用 WaitHandle 而非 Mutex。Use a WaitHandle other than Mutex.
- callBack
- WaitOrTimerCallback
向 waitObject
参数发出信号时调用的 WaitOrTimerCallback 委托。The WaitOrTimerCallback delegate to call when the waitObject
parameter is signaled.
- state
- Object
传递给委托的对象。The object that is passed to the delegate.
- millisecondsTimeOutInterval
- Int32
以毫秒为单位的超时。The time-out in milliseconds. 如果 millisecondsTimeOutInterval
参数为 0(零),函数将测试对象的状态并立即返回。If the millisecondsTimeOutInterval
parameter is 0 (zero), the function tests the object's state and returns immediately. 如果 millisecondsTimeOutInterval
为 -1,则函数的超时间隔永远不过期。If millisecondsTimeOutInterval
is -1, the function's time-out interval never elapses.
- executeOnlyOnce
- Boolean
如果为 true
,表示在调用了委托后,线程将不再在 waitObject
参数上等待;如果为 false
,表示每次完成等待操作后都重置计时器,直到注销等待。true
to indicate that the thread will no longer wait on the waitObject
parameter after the delegate has been called; false
to indicate that the timer is reset every time the wait operation completes until the wait is unregistered.
返回
封装本机句柄的 RegisteredWaitHandle。The RegisteredWaitHandle that encapsulates the native handle.
异常
millisecondsTimeOutInterval
参数小于 -1。The millisecondsTimeOutInterval
parameter is less than -1.
注解
使用完此方法返回的 RegisteredWaitHandle 后,调用其 @no__t 1 方法以释放对等待句柄的引用。When you are finished using the RegisteredWaitHandle that is returned by this method, call its RegisteredWaitHandle.Unregister method to release references to the wait handle. 建议始终调用 RegisteredWaitHandle.Unregister 方法,即使为 @no__t 指定 true
。We recommend that you always call the RegisteredWaitHandle.Unregister method, even if you specify true
for executeOnlyOnce
. 如果调用 RegisteredWaitHandle.Unregister 方法,而不是根据注册的等待句柄的终结器,则垃圾回收的工作效率更高。Garbage collection works more efficiently if you call the RegisteredWaitHandle.Unregister method instead of depending on the registered wait handle's finalizer.
@No__t-0 方法会将指定的委托排队到线程池。The RegisterWaitForSingleObject method queues the specified delegate to the thread pool. 当发生以下情况之一时,工作线程将执行委托:A worker thread will execute the delegate when one of the following occurs:
指定的对象处于终止状态。The specified object is in the signaled state.
超时间隔已过去。The time-out interval elapses.
@No__t-0 方法检查指定对象的 @no__t 的当前状态。The RegisterWaitForSingleObject method checks the current state of the specified object's WaitHandle. 如果对象的状态为信号,则该方法将注册等待操作。If the object's state is unsignaled, the method registers a wait operation. 等待操作由线程池中的一个线程执行。The wait operation is performed by a thread from the thread pool. 当对象的状态变为 "已终止" 或超时间隔结束时,工作线程会执行委托。The delegate is executed by a worker thread when the object's state becomes signaled or the time-out interval elapses. 如果 @no__t 参数不为0(零),并且 @no__t 参数 @no__t 为-2,则每当事件发出信号或超时间隔结束时,将重置计时器。If the timeOutInterval
parameter is not 0 (zero) and the executeOnlyOnce
parameter is false
, the timer is reset every time the event is signaled or the time-out interval elapses.
重要
使用 @no__t 为-1 的 @no__t 不会为回调提供互斥,因为基础 Windows API 使用默认的 WT_EXECUTEDEFAULT
标志,因此每个回调都在单独的线程池线程上进行调度。Using a Mutex for waitObject
does not provide mutual exclusion for the callbacks because the underlying Windows API uses the default WT_EXECUTEDEFAULT
flag, so each callback is dispatched on a separate thread pool thread. 请使用最大计数为1的 Semaphore,而不是 Mutex。Instead of a Mutex, use a Semaphore with a maximum count of 1.
若要取消等待操作,请调用 RegisteredWaitHandle.Unregister 方法。To cancel the wait operation, call the RegisteredWaitHandle.Unregister method.
等待线程使用 Win32 WaitForMultipleObjects
函数来监视已注册的等待操作。The wait thread uses the Win32 WaitForMultipleObjects
function to monitor registered wait operations. 因此,如果必须在多个调用中使用相同的本机操作系统句柄 RegisterWaitForSingleObject,则必须使用 Win32 DuplicateHandle
函数复制句柄。Therefore, if you must use the same native operating system handle in multiple calls to RegisterWaitForSingleObject, you must duplicate the handle using the Win32 DuplicateHandle
function. 请注意,不应脉冲传递到 RegisterWaitForSingleObject 的事件对象,因为等待线程可能检测不到事件在重置前已发出信号。Note that you should not pulse an event object passed to RegisterWaitForSingleObject, because the wait thread might not detect that the event is signaled before it is reset.
在返回之前,函数会修改某些类型的同步对象的状态。Before returning, the function modifies the state of some types of synchronization objects. 仅对其终止状态导致等待条件得到满足的对象进行修改。Modification occurs only for the object whose signaled state caused the wait condition to be satisfied. 例如,信号量的计数减少1。For example, the count of a semaphore is decreased by one.
版本信息Version Information
从 .NET Framework 版本2.0 开始,@no__t 属性值将传播到使用 @no__t 1 方法排队的工作线程。Starting with the .NET Framework version 2.0, the Thread.CurrentPrincipal property value is propagated to worker threads queued using the RegisterWaitForSingleObject method. 在早期版本中,不传播主体信息。In earlier versions, the principal information is not propagated.
另请参阅
RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, Int64, Boolean)
注册一个等待 WaitHandle 的委托,并指定一个 64 位有符号整数来表示超时值(以毫秒为单位)。Registers a delegate to wait for a WaitHandle, specifying a 64-bit signed integer for the time-out in milliseconds.
public:
static System::Threading::RegisteredWaitHandle ^ RegisterWaitForSingleObject(System::Threading::WaitHandle ^ waitObject, System::Threading::WaitOrTimerCallback ^ callBack, System::Object ^ state, long millisecondsTimeOutInterval, bool executeOnlyOnce);
public static System.Threading.RegisteredWaitHandle RegisterWaitForSingleObject (System.Threading.WaitHandle waitObject, System.Threading.WaitOrTimerCallback callBack, object state, long millisecondsTimeOutInterval, bool executeOnlyOnce);
static member RegisterWaitForSingleObject : System.Threading.WaitHandle * System.Threading.WaitOrTimerCallback * obj * int64 * bool -> System.Threading.RegisteredWaitHandle
Public Shared Function RegisterWaitForSingleObject (waitObject As WaitHandle, callBack As WaitOrTimerCallback, state As Object, millisecondsTimeOutInterval As Long, executeOnlyOnce As Boolean) As RegisteredWaitHandle
参数
- waitObject
- WaitHandle
要注册的 WaitHandle。The WaitHandle to register. 使用 WaitHandle 而非 Mutex。Use a WaitHandle other than Mutex.
- callBack
- WaitOrTimerCallback
向 waitObject
参数发出信号时调用的 WaitOrTimerCallback 委托。The WaitOrTimerCallback delegate to call when the waitObject
parameter is signaled.
- state
- Object
传递给委托的对象。The object passed to the delegate.
- millisecondsTimeOutInterval
- Int64
以毫秒为单位的超时。The time-out in milliseconds. 如果 millisecondsTimeOutInterval
参数为 0(零),函数将测试对象的状态并立即返回。If the millisecondsTimeOutInterval
parameter is 0 (zero), the function tests the object's state and returns immediately. 如果 millisecondsTimeOutInterval
为 -1,则函数的超时间隔永远不过期。If millisecondsTimeOutInterval
is -1, the function's time-out interval never elapses.
- executeOnlyOnce
- Boolean
如果为 true
,表示在调用了委托后,线程将不再在 waitObject
参数上等待;如果为 false
,表示每次完成等待操作后都重置计时器,直到注销等待。true
to indicate that the thread will no longer wait on the waitObject
parameter after the delegate has been called; false
to indicate that the timer is reset every time the wait operation completes until the wait is unregistered.
返回
封装本机句柄的 RegisteredWaitHandle。The RegisteredWaitHandle that encapsulates the native handle.
异常
millisecondsTimeOutInterval
参数小于 -1。The millisecondsTimeOutInterval
parameter is less than -1.
注解
使用完此方法返回的 RegisteredWaitHandle 后,调用其 @no__t 1 方法以释放对等待句柄的引用。When you are finished using the RegisteredWaitHandle that is returned by this method, call its RegisteredWaitHandle.Unregister method to release references to the wait handle. 建议始终调用 RegisteredWaitHandle.Unregister 方法,即使为 @no__t 指定 true
。We recommend that you always call the RegisteredWaitHandle.Unregister method, even if you specify true
for executeOnlyOnce
. 如果调用 RegisteredWaitHandle.Unregister 方法,而不是根据注册的等待句柄的终结器,则垃圾回收的工作效率更高。Garbage collection works more efficiently if you call the RegisteredWaitHandle.Unregister method instead of depending on the registered wait handle's finalizer.
@No__t-0 方法会将指定的委托排队到线程池。The RegisterWaitForSingleObject method queues the specified delegate to the thread pool. 当发生以下情况之一时,工作线程将执行委托:A worker thread will execute the delegate when one of the following occurs:
指定的对象处于终止状态。The specified object is in the signaled state.
超时间隔已过去。The time-out interval elapses.
@No__t-0 方法检查指定对象的 @no__t 的当前状态。The RegisterWaitForSingleObject method checks the current state of the specified object's WaitHandle. 如果对象的状态为信号,则该方法将注册等待操作。If the object's state is unsignaled, the method registers a wait operation. 等待操作由线程池中的一个线程执行。The wait operation is performed by a thread from the thread pool. 当对象的状态变为 "已终止" 或超时间隔结束时,工作线程会执行委托。The delegate is executed by a worker thread when the object's state becomes signaled or the time-out interval elapses. 如果 @no__t 参数不为0(零),并且 @no__t 参数 @no__t 为-2,则每当事件发出信号或超时间隔结束时,将重置计时器。If the timeOutInterval
parameter is not 0 (zero) and the executeOnlyOnce
parameter is false
, the timer is reset every time the event is signaled or the time-out interval elapses.
重要
使用 @no__t 为-1 的 @no__t 不会为回调提供互斥,因为基础 Windows API 使用默认的 WT_EXECUTEDEFAULT
标志,因此每个回调都在单独的线程池线程上进行调度。Using a Mutex for waitObject
does not provide mutual exclusion for the callbacks because the underlying Windows API uses the default WT_EXECUTEDEFAULT
flag, so each callback is dispatched on a separate thread pool thread. 请使用最大计数为1的 Semaphore,而不是 Mutex。Instead of a Mutex, use a Semaphore with a maximum count of 1.
若要取消等待操作,请调用 RegisteredWaitHandle.Unregister 方法。To cancel the wait operation, call the RegisteredWaitHandle.Unregister method.
等待线程使用 Win32 WaitForMultipleObjects
函数来监视已注册的等待操作。The wait thread uses the Win32 WaitForMultipleObjects
function to monitor registered wait operations. 因此,如果必须在多个调用中使用相同的本机操作系统句柄 RegisterWaitForSingleObject,则必须使用 Win32 DuplicateHandle
函数复制句柄。Therefore, if you must use the same native operating system handle in multiple calls to RegisterWaitForSingleObject, you must duplicate the handle using the Win32 DuplicateHandle
function. 请注意,不应脉冲传递到 RegisterWaitForSingleObject 的事件对象,因为等待线程可能检测不到事件在重置前已发出信号。Note that you should not pulse an event object passed to RegisterWaitForSingleObject, because the wait thread might not detect that the event is signaled before it is reset.
在返回之前,函数会修改某些类型的同步对象的状态。Before returning, the function modifies the state of some types of synchronization objects. 仅对其终止状态导致等待条件得到满足的对象进行修改。Modification occurs only for the object whose signaled state caused the wait condition to be satisfied. 例如,信号量的计数减少1。For example, the count of a semaphore is decreased by one.
版本信息Version Information
从 .NET Framework 版本2.0 开始,@no__t 属性值将传播到使用 @no__t 1 方法排队的工作线程。Starting with the .NET Framework version 2.0, the Thread.CurrentPrincipal property value is propagated to worker threads queued using the RegisterWaitForSingleObject method. 在早期版本中,不传播主体信息。In earlier versions, the principal information is not propagated.