Freigeben über


WaitCallback-Delegat

Stellt die Rückrufmethode dar, die von einem Thread des Threadpools ausgeführt werden soll.

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Delegate Sub WaitCallback ( _
    state As Object _
)
'Usage
Dim instance As New WaitCallback(AddressOf HandlerMethod)
[ComVisibleAttribute(true)] 
public delegate void WaitCallback (
    Object state
)
[ComVisibleAttribute(true)] 
public delegate void WaitCallback (
    Object^ state
)
/** @delegate */
/** @attribute ComVisibleAttribute(true) */ 
public delegate void WaitCallback (
    Object state
)
JScript unterstützt die Verwendung von Delegaten, aber nicht die Deklaration von neuen Delegaten.

Parameter

  • state
    Ein Objekt, das die von der Rückrufmethode zu verwendenden Informationen enthält.

Hinweise

WaitCallback stellt eine Rückrufmethode dar, die für einen ThreadPool-Thread ausgeführt werden soll. Erstellen Sie den Delegaten, indem Sie die Rückrufmethode an den WaitCallback-Konstruktor übergeben. Die Methode muss über die hier dargestellte Signatur verfügen.

Fügen Sie den auszuführenden Task der Warteschlange hinzu, indem Sie den WaitCallback-Delegaten an ThreadPool.QueueUserWorkItem übergeben. Die Rückrufmethode wird ausgeführt, wenn ein Thread des Threadpools verfügbar wird.

Hinweis

Benutzer von Visual Basic können den WaitCallback-Konstruktor auslassen und stattdessen beim Übergeben der Rückrufmethode an QueueUserWorkItem den AddressOf-Operator verwenden. Visual Basic ruft automatisch den richtigen Delegatkonstruktor auf.

Wenn Sie Informationen an die Rückrufmethode übergeben möchten, erstellen Sie ein Objekt mit den erforderlichen Informationen und übergeben es zu dem Zeitpunkt an QueueUserWorkItem, zu dem Sie den auszuführenden Task der Warteschlange hinzufügen. Bei jeder Ausführung der Rückrufmethode enthält der state-Parameter dieses Objekt.

Weitere Informationen über die Verwendung von Rückrufen zum Synchronisieren von Threadpoolthreads finden Sie unter Verwalteter Threadpool.

Beispiel

Imports System
Imports System.Threading

Public Class Example

    <MTAThread> _
    Public Shared Sub Main()
        ' Queue the task.
        ThreadPool.QueueUserWorkItem( _
            New WaitCallback(AddressOf ThreadProc) _
            )
        ' Note that you do not have to create the WaitCallback delegate
        ' explicitly in Visual Basic.  The following line also queues 
        ' the task:
        'ThreadPool.QueueUserWorkItem(AddressOf ThreadProc)
        
        Console.WriteLine("Main thread does some work, then sleeps.")
        ' If you comment out the Sleep, the main thread exits before
        ' the thread pool task runs.  The thread pool uses background
        ' threads, which do not keep the application running.  (This
        ' is a simple example of a race condition.)
        Thread.Sleep(1000)

        Console.WriteLine("Main thread exits.")
    End Sub

    ' This thread procedure performs the task.
    Shared Sub ThreadProc(stateInfo As Object)
        ' No state object was passed to QueueUserWorkItem, so 
        ' stateInfo is null.
        Console.WriteLine("Hello from the thread pool.")
    End Sub
End Class
using System;
using System.Threading;
public class Example {
    public static void Main() {
        // Queue the task.
        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
        
        Console.WriteLine("Main thread does some work, then sleeps.");
        // If you comment out the Sleep, the main thread exits before
        // the thread pool task runs.  The thread pool uses background
        // threads, which do not keep the application running.  (This
        // is a simple example of a race condition.)
        Thread.Sleep(1000);

        Console.WriteLine("Main thread exits.");
    }

    // This thread procedure performs the task.
    static void ThreadProc(Object stateInfo) {
        // No state object was passed to QueueUserWorkItem, so 
        // stateInfo is null.
        Console.WriteLine("Hello from the thread pool.");
    }
}
using namespace System;
using namespace System::Threading;
ref class Example
{
public:

   // This thread procedure performs the task.
   static void ThreadProc( Object^ stateInfo )
   {
      
      // No state object was passed to QueueUserWorkItem, so 
      // stateInfo is 0.
      Console::WriteLine( "Hello from the thread pool." );
   }

};

int main()
{
   
   // Queue the task.
   ThreadPool::QueueUserWorkItem( gcnew WaitCallback( Example::ThreadProc ) );
   Console::WriteLine( "Main thread does some work, then sleeps." );
   
   // If you comment out the Sleep, the main thread exits before
   // the thread pool task runs.  The thread pool uses background
   // threads, which do not keep the application running.  (This
   // is a simple example of a race condition.)
   Thread::Sleep( 1000 );
   Console::WriteLine( "Main thread exits." );
   return 0;
}
import System.*;
import System.Threading.*;
import System.Threading.Thread;

public class Example
{
    public static void main(String[] args)
    {
        // Queue the task.
        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
        Console.WriteLine("Main thread does some work, then sleeps.");

        // If you comment out the Sleep, the main thread exits before
        // the thread pool task runs.  The thread pool uses background
        // threads, which do not keep the application running.  (This
        // is a simple example of a race condition.)
        Thread.Sleep(1000);
        Console.WriteLine("Main thread exits.");
    } //main

    // This thread procedure performs the task.
    static void ThreadProc(Object stateInfo)
    {
        // No state object was passed to QueueUserWorkItem, so 
        // stateInfo is null.
        Console.WriteLine("Hello from the thread pool.");
    } //ThreadProc
} //Example

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

System.Threading-Namespace
ThreadPool-Klasse

Weitere Ressourcen

Threads und Threading
Verwalteter Threadpool