WaitCallback Delegate
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Represents a callback method to be executed by a thread pool thread.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- state
- Type: System::Object
An object that contains 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 in the Syntax section.
Queue your task for execution by passing the WaitCallback delegate to the ThreadPool::QueueUserWorkItem method. Your callback method executes when a thread pool thread becomes available.
Note: |
|---|
Visual Basic and C# users can omit the WaitCallback constructor, because the compilers infer the delegate type automatically and supply the correct constructor. In Visual Basic, use the AddressOf operator when passing the callback method to QueueUserWorkItem. |
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. Every time your callback method executes, the state parameter contains this object.
For more information about how to use 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.
The example displays its output in a TextBlock on the UI thread. To access the TextBlock from the callback thread, the example uses the Dispatcher property to obtain a Dispatcher object for the TextBlock, and then uses the Dispatcher::BeginInvoke method to make the cross-thread call.
Note: