共用方式為


WaitOrTimerCallback 代理人

定義

表示 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

true,表示 WaitHandle 逾時;false,表示收到信號。

屬性

範例

下列範例示範如何使用 WaitOrTimerCallback 委派來表示在發出等候控制碼時所執行的回呼方法。

此範例也會示範如何在發出指定的等候控制碼時,使用 RegisterWaitForSingleObject 方法來執行指定的回呼方法。 在此範例中,回呼方法是 WaitProc ,而等候控制碼是 AutoResetEvent

此範例會定義類別 TaskInfo ,以保存在執行時傳遞至回呼的資訊。 此範例會 TaskInfo 建立 物件,並指派一些字串資料。 方法傳 RegisterWaitForSingleObject 回的 TaskInfo 會指派給 Handle 物件的欄位,讓回呼方法可以存取 RegisteredWaitHandleRegisteredWaitHandle

除了 TaskInfo 物件之外,呼叫 RegisterWaitForSingleObject 方法還會指定 AutoResetEvent 工作等候的時間、代表 WaitProc 回呼方法的委派、 WaitOrTimerCallback 一秒的逾時間隔和多個回呼。

當主執行緒藉由呼叫 其 Set 方法發出訊 AutoResetEvent 號時, WaitOrTimerCallback 會叫用委派。 方法 WaitProc 會測試 RegisteredWaitHandle 以判斷是否發生逾時。 如果因為已發出等候控制碼而叫用回呼,則方法會取消註冊 RegisteredWaitHandleWaitProc 並停止進一步的回呼。 在逾時的情況下,工作會繼續等候。 方法會 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 建構函式,以建立委派。 您的方法必須有此處顯示的簽章。

將委派和 WaitHandle 傳遞 WaitOrTimerCallbackThreadPool.RegisterWaitForSingleObject ,以建立已註冊的等候控制碼。 每次逾時或發出訊號時, WaitHandle 您的回呼方法都會執行。

注意

Visual Basic 使用者可以省略建 WaitOrTimerCallback 構函式,只要在將回呼方法傳遞至 RegisterWaitForSingleObject 時使用 AddressOf 運算子即可。 Visual Basic 會自動呼叫正確的委派建構函式。

如果您想要將資訊傳遞至回呼方法,請建立包含必要資訊的物件,並在建立已註冊的等候控制碼時傳遞至 RegisterWaitForSingleObject 。 每次回呼方法執行時, state 參數都會包含這個物件。

如需使用回呼方法來同步處理執行緒集區執行緒的詳細資訊,請參閱 受控執行緒集區

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

另請參閱