RegisteredWaitHandle 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示在调用 RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, UInt32, Boolean)时已注册的句柄。 此类不能被继承。
public ref class RegisteredWaitHandle sealed : MarshalByRefObject
public ref class RegisteredWaitHandle sealed
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public sealed class RegisteredWaitHandle : MarshalByRefObject
public sealed class RegisteredWaitHandle
public sealed class RegisteredWaitHandle : MarshalByRefObject
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class RegisteredWaitHandle : MarshalByRefObject
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
type RegisteredWaitHandle = class
inherit MarshalByRefObject
type RegisteredWaitHandle = class
type RegisteredWaitHandle = class
inherit MarshalByRefObject
[<System.Runtime.InteropServices.ComVisible(true)>]
type RegisteredWaitHandle = class
inherit MarshalByRefObject
Public NotInheritable Class RegisteredWaitHandle
Inherits MarshalByRefObject
Public NotInheritable Class RegisteredWaitHandle
- 继承
- 继承
-
RegisteredWaitHandle
- 属性
示例
以下示例演示如何使用 a RegisteredWaitHandle 来确定调用回调方法的原因,以及如何在回调发生时取消注册任务,因为等待句柄已发出信号。
该示例还演示如何在发出指定的等待句柄信号时使用 RegisterWaitForSingleObject 该方法执行指定的回调方法。 在此示例中,回调方法是 WaitProc,等待句柄是一个 AutoResetEvent。
该示例定义一个 TaskInfo 类,用于保存在执行回调时传递给回调的信息。 该示例创建一个 TaskInfo 对象并为其分配一些字符串数据。
RegisteredWaitHandle方法返回RegisterWaitForSingleObject的函数将分配给Handle对象的字段TaskInfo,以便回调方法有权访问该RegisteredWaitHandle对象。
除了指定TaskInfo为传递给回调方法的对象外,对方法的调用RegisterWaitForSingleObject还指定AutoResetEvent任务将等待、表示回调方法的WaitOrTimerCallback委托、WaitProc一秒钟超时间隔和多个回调。
当主线程通过调用其AutoResetEvent方法发出信号Set时,WaitOrTimerCallback将调用委托。 该方法 WaitProc 测试 RegisteredWaitHandle 以确定是否发生超时。 如果调用回调是因为等待句柄已发出信号,该方法 WaitProc 将取消注册 RegisteredWaitHandle该回调,从而停止其他回调。 如果超时,任务将继续等待。 该方法 WaitProc 通过将消息打印到控制台结束。
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
方法
| 名称 | 说明 |
|---|---|
| CreateObjRef(Type) |
创建一个对象,其中包含生成用于与远程对象通信的代理所需的所有相关信息。 (继承自 MarshalByRefObject) |
| Equals(Object) |
确定指定的对象是否等于当前对象。 (继承自 Object) |
| Finalize() |
允许对象在垃圾回收回收资源之前尝试释放资源并执行其他清理操作。 |
| GetHashCode() |
用作默认哈希函数。 (继承自 Object) |
| GetLifetimeService() |
已过时.
检索控制此实例的生存期策略的当前生存期服务对象。 (继承自 MarshalByRefObject) |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| InitializeLifetimeService() |
已过时.
获取生存期服务对象来控制此实例的生存期策略。 (继承自 MarshalByRefObject) |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| MemberwiseClone(Boolean) |
创建当前 MarshalByRefObject 对象的浅表副本。 (继承自 MarshalByRefObject) |
| ToString() |
返回一个表示当前对象的字符串。 (继承自 Object) |
| Unregister(WaitHandle) |
取消方法颁发的 RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, UInt32, Boolean) 已注册等待操作。 |
适用于
线程安全性
此类型是线程安全的。