Represents a callback method to be executed by a thread pool thread.
Namespace:
System.Threading
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
<ComVisibleAttribute(True)> _
Public Delegate Sub WaitCallback ( _
state As Object _
)
Dim instance As New WaitCallback(AddressOf HandlerMethod)
[ComVisibleAttribute(true)]
public delegate void WaitCallback(
Object state
)
[ComVisibleAttribute(true)]
public delegate void WaitCallback(
Object^ state
)
JScript does not support delegates.
Parameters
- state
- Type: System..::.Object
An object containing information to be used by the callback method.
WaitCallback represents a callback method that you want to execute on a ThreadPool thread. Create the delegate by passing your callback method to the WaitCallback constructor. Your method must have the signature shown here.
Queue your task for execution by passing the WaitCallback delegate to ThreadPool..::.QueueUserWorkItem. Your callback method executes when a thread pool thread becomes available.
vb# Note: |
|---|
Visual Basic users can omit the WaitCallback constructor, and simply use the AddressOf operator when passing the callback method to QueueUserWorkItem. Visual Basic automatically calls the correct delegate constructor. |
If you want to pass information to your callback method, create an object that contains the necessary information and pass it to QueueUserWorkItem when you queue your task for execution. Each time your callback method executes, the state parameter contains this object.
For more information about using callbacks to synchronize thread pool threads, see The Managed Thread Pool.
The following example shows how to use the WaitCallback delegate to queue a task for execution by the thread pool. The code example uses the ThreadPool..::.QueueUserWorkItem(WaitCallback) method overload to queue a task, which is represented by a WaitCallback that wraps the ThreadProc method, to execute when a thread becomes available. No task information is supplied with this overload. Therefore, the information that is available to the ThreadProc method is limited to the object the method belongs to.
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;
}
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
.NET Compact Framework
Supported in: 3.5, 2.0, 1.0
XNA Framework
Supported in: 3.0, 2.0, 1.0
Reference
Other Resources