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
- 屬性
範例
下列範例示範如何使用 RegisteredWaitHandle 來判斷呼叫回呼方法的原因,以及如何在回呼發生時取消註冊工作,因為等候控制碼已發出訊號。
此範例也會示範如何在發出指定的等候控制碼時,使用 RegisterWaitForSingleObject 方法來執行指定的回呼方法。 在此範例中,回呼方法是 WaitProc
,而等候控制碼是 AutoResetEvent 。
此範例會定義類別 TaskInfo
,以保存在執行時傳遞至回呼的資訊。 此範例會 TaskInfo
建立 物件,並指派一些字串資料。 方法傳 RegisterWaitForSingleObject 回的 TaskInfo
會指派給 Handle
物件的欄位,讓回呼方法可以存取 RegisteredWaitHandle 。 RegisteredWaitHandle
除了指定要 TaskInfo
傳遞至回呼方法的物件之外,對 方法的呼叫 RegisterWaitForSingleObject 還會指定要 AutoResetEvent 等候的工作、代表 WaitProc
回呼方法的委派、 WaitOrTimerCallback 一秒逾時間隔和多個回呼。
當主執行緒藉由呼叫 其 Set 方法發出訊 AutoResetEvent 號時, WaitOrTimerCallback 會叫用委派。 方法 WaitProc
會測試 RegisteredWaitHandle 以判斷是否發生逾時。 如果因為已發出等候控制碼而叫用回呼,則方法會取消註冊 RegisteredWaitHandle , WaitProc
停止其他回呼。 在逾時的情況下,工作會繼續等候。 方法會 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
方法
CreateObjRef(Type) |
建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。 (繼承來源 MarshalByRefObject) |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
Finalize() |
允許物件在記憶體回收進行回收之前,嘗試釋放資源並執行其他清除作業。 |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetLifetimeService() |
已淘汰.
擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。 (繼承來源 MarshalByRefObject) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
InitializeLifetimeService() |
已淘汰.
取得存留期服務物件,以控制這個執行個體的存留期原則。 (繼承來源 MarshalByRefObject) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
MemberwiseClone(Boolean) |
建立目前 MarshalByRefObject 物件的淺層複本。 (繼承來源 MarshalByRefObject) |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |
Unregister(WaitHandle) |
將 RegisterWaitForSingleObject(WaitHandle, WaitOrTimerCallback, Object, UInt32, Boolean) 方法所發出之已登錄等候作業取消。 |
適用於
執行緒安全性
此型別具備執行緒安全。