ThreadPool.QueueUserWorkItem Method (WaitCallback)
Queues a method for execution. The method executes when a thread pool thread becomes available.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- callBack
-
Type:
System.Threading.WaitCallback
A WaitCallback that represents the method to be executed.
Return Value
Type: System.Booleantrue if the method is successfully queued; NotSupportedException is thrown if the work item could not be queued.
| Exception | Condition |
|---|---|
| ArgumentNullException | callBack is null. |
| NotSupportedException | The common language runtime (CLR) is hosted, and the host does not support this action. |
You can place data required by the queued method in the instance fields of the class in which the method is defined, or you can use the QueueUserWorkItem(WaitCallback, Object) overload that accepts an object containing the necessary data.
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. |
In the .NET Framework version 2.0, the Thread.CurrentPrincipal property value is propagated to worker threads queued using the QueueUserWorkItem method. In earlier versions, the principal information is not propagated.
The following example uses the QueueUserWorkItem(WaitCallback) method overload to queue a task, which is represented by 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.
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
