ThreadPool.QueueUserWorkItem Method
.NET Framework 1.1
Queues a method for execution. The method executes when a thread pool thread becomes available.
Overload List
Queues a method for execution. The method executes when a thread pool thread becomes available.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function QueueUserWorkItem(WaitCallback) As Boolean
[C#] public static bool QueueUserWorkItem(WaitCallback);
[C++] public: static bool QueueUserWorkItem(WaitCallback*);
[JScript] public static function QueueUserWorkItem(WaitCallback) : Boolean;
Queues a method for execution, and specifies an object containing data to be used by the method. The method executes when a thread pool thread becomes available.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function QueueUserWorkItem(WaitCallback, Object) As Boolean
[C#] public static bool QueueUserWorkItem(WaitCallback, object);
[C++] public: static bool QueueUserWorkItem(WaitCallback*, Object*);
[JScript] public static function QueueUserWorkItem(WaitCallback, Object) : Boolean;
Example
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of QueueUserWorkItem. For other examples that might be available, see the individual overload topics.
[Visual Basic] ' This example shows how to create an object containing task ' information, and pass that object to a task queued for ' execution by the thread pool. Imports System Imports System.Threading ' TaskInfo holds state information for a task that will be ' executed by a ThreadPool thread. Public Class TaskInfo ' State information for the task. These members ' can be implemented as read-only properties, read/write ' properties with validation, and so on, as required. Public Boilerplate As String Public Value As Integer ' Public constructor provides an easy way to supply all ' the information needed for the task. Public Sub New(text As String, number As Integer) Boilerplate = text Value = number End Sub End Class Public Class Example Public Shared Sub Main() ' Create an object containing the information needed ' for the task. Dim ti As New TaskInfo("This report displays the number {0}.", 42) ' Queue the task and data. If ThreadPool.QueueUserWorkItem( _ New WaitCallback(AddressOf ThreadProc), ti) Then Console.WriteLine("Main thread does some work, then sleeps.") ' If you comment out the Sleep, the main thread exits before ' the ThreadPool task has a chance to run. ThreadPool 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.") Else Console.WriteLine("Unable to queue ThreadPool request.") End If End Sub ' The thread procedure performs the independent task, in this case ' formatting and printing a very simple report. ' Shared Sub ThreadProc(stateInfo As Object) Dim ti As TaskInfo = CType(stateInfo, TaskInfo) Console.WriteLine(ti.Boilerplate, ti.Value) End Sub End Class [C#] // This example shows how to create an object containing task // information, and pass that object to a task queued for // execution by the thread pool. using System; using System.Threading; // TaskInfo holds state information for a task that will be // executed by a ThreadPool thread. public class TaskInfo { // State information for the task. These members // can be implemented as read-only properties, read/write // properties with validation, and so on, as required. public string Boilerplate; public int Value; // Public constructor provides an easy way to supply all // the information needed for the task. public TaskInfo(string text, int number) { Boilerplate = text; Value = number; } } public class Example { public static void Main() { // Create an object containing the information needed // for the task. TaskInfo ti = new TaskInfo("This report displays the number {0}.", 42); // Queue the task and data. if (ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), ti)) { Console.WriteLine("Main thread does some work, then sleeps."); // If you comment out the Sleep, the main thread exits before // the ThreadPool task has a chance to run. ThreadPool 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."); } else { Console.WriteLine("Unable to queue ThreadPool request."); } } // The thread procedure performs the independent task, in this case // formatting and printing a very simple report. // static void ThreadProc(Object stateInfo) { TaskInfo ti = (TaskInfo) stateInfo; Console.WriteLine(ti.Boilerplate, ti.Value); } } [C++] // This example shows how to create an Object* containing task // information, and pass that Object* to a task queued for // execution by the thread pool. #using <mscorlib.dll> using namespace System; using namespace System::Threading; // TaskInfo holds state information for a task that will be // executed by a ThreadPool thread. public __gc class TaskInfo { // State information for the task. These members // can be implemented as read-only properties, read/write // properties with validation, and so on, as required. public: String* Boilerplate; int Value; // Public constructor provides an easy way to supply all // the information needed for the task. TaskInfo(String* text, int number) { Boilerplate = text; Value = number; } }; public __gc struct Example { // The thread procedure performs the independent task, in this case // formatting and printing a very simple report. // static void ThreadProc(Object* stateInfo) { TaskInfo* ti = dynamic_cast<TaskInfo*>(stateInfo); Console::WriteLine(ti->Boilerplate, __box(ti->Value)); } }; int main() { // Create an object containing the information needed // for the task. TaskInfo* ti = new TaskInfo(S"This report displays the number {0}.", 42); // Queue the task and data. if (ThreadPool::QueueUserWorkItem(new WaitCallback(0, Example::ThreadProc), ti)) { Console::WriteLine(S"Main thread does some work, then sleeps."); // If you comment out the Sleep, the main thread exits before // the ThreadPool task has a chance to run. ThreadPool uses // background threads, which do not keep the application // running. (This is a simple example of a race condition.) Thread::Sleep(1000); Console::WriteLine(S"Main thread exits."); } else { Console::WriteLine(S"Unable to queue ThreadPool request."); } return 0; }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
See Also
ThreadPool Class | ThreadPool Members | System.Threading Namespace