WaitOrTimerCallback 委托
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示当 WaitHandle 超时或终止时要调用的方法。
public delegate void WaitOrTimerCallback(System::Object ^ state, bool timedOut);
public delegate void WaitOrTimerCallback(object? state, bool timedOut);
public delegate void WaitOrTimerCallback(object state, bool timedOut);
[System.Runtime.InteropServices.ComVisible(true)]
public delegate void WaitOrTimerCallback(object state, bool timedOut);
type WaitOrTimerCallback = delegate of obj * bool -> unit
[<System.Runtime.InteropServices.ComVisible(true)>]
type WaitOrTimerCallback = delegate of obj * bool -> unit
Public Delegate Sub WaitOrTimerCallback(state As Object, timedOut As Boolean)
参数
- state
- Object
一个对象,包含回调方法在每次执行时要使用的信息。
- timedOut
- Boolean
如果 WaitHandle 超时,则为 true
;如果其终止,则为 false
。
- 属性
示例
以下示例演示如何使用 WaitOrTimerCallback 委托来表示在发出等待句柄信号时执行的回调方法。
该示例还演示如何在发出指定的等待句柄信号时使用 RegisterWaitForSingleObject 方法执行指定的回调方法。 在此示例中,回调方法是 WaitProc
,等待句柄是 AutoResetEvent。
该示例定义一个 TaskInfo
类,用于保存在执行回调时传递给回调的信息。 该示例创建一个 TaskInfo
对象,并为其分配一些字符串数据。
RegisteredWaitHandle方法返回RegisterWaitForSingleObject的 分配给 Handle
对象的 字段TaskInfo
,以便回调方法有权访问 RegisteredWaitHandle。
除了 TaskInfo
对象之外,对 RegisterWaitForSingleObject 方法的调用还 AutoResetEvent 指定任务等待、表示 WaitOrTimerCallback 回调方法的 WaitProc
委托、一秒超时间隔和多个回调。
当主线程通过调用其 Set 方法向 发出信号AutoResetEvent时,WaitOrTimerCallback将调用委托。 方法 WaitProc
测试 RegisteredWaitHandle 以确定是否发生超时。 如果由于已发出等待句柄信号而调用回调,则 WaitProc
方法将取消注册 RegisteredWaitHandle,从而停止进一步的回调。 如果超时,任务将继续等待。 方法 WaitProc
以将消息打印到控制台结束。
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
注解
WaitOrTimerCallback 表示要在注册的等待句柄超时或发出信号时执行的回调方法。 通过将回调方法传递给 WaitOrTimerCallback 构造函数来创建委托。 方法必须具有此处所示的签名。
通过将委托和 WaitHandleThreadPool.RegisterWaitForSingleObject传递到 来创建已注册的WaitOrTimerCallback等待句柄。 回调方法在每次超时或发出信号时 WaitHandle 执行。
注意
Visual Basic 用户可以省略 WaitOrTimerCallback 构造函数,只需在 AddressOf
将回调方法 RegisterWaitForSingleObject传递给 时使用 运算符。 Visual Basic 自动调用正确的委托构造函数。
如果要将信息传递给回调方法,请创建一个包含必要信息的对象,并在创建注册的等待句柄时将其 RegisterWaitForSingleObject 传递给 。 每次执行回调方法时 state
,参数都包含此 对象。
有关使用回调方法同步线程池线程的详细信息,请参阅 托管线程池。
扩展方法
GetMethodInfo(Delegate) |
获取指示指定委托表示的方法的对象。 |