ThreadPool.QueueUserWorkItem Method (WaitCallback)
Queues a method for execution. The method executes when a thread pool thread becomes available.
Assembly: mscorlib (in mscorlib.dll)
'Declaration <SecuritySafeCriticalAttribute> _ Public Shared Function QueueUserWorkItem ( _ callBack As WaitCallback _ ) As Boolean
- Type: System.Threading.WaitCallback
The method to execute.
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.
The following example uses the 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.
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.
To run this example, see Building Examples That Use a Demo Method and a TextBlock Control.
Imports System.Threading Class Example ' This is the UI element that receives the output from the example. Private Shared outputBlock As System.Windows.Controls.TextBlock ' The Demo method runs the example. It saves the TextBlock that is ' used for output, and sets up an event handler to start tasks. Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) Example.outputBlock = outputBlock outputBlock.Text = "Click here to start a background task." & vbCrLf ' Set up an event handler to start a task when the TextBlock ' is clicked. AddHandler outputBlock.MouseLeftButtonUp, AddressOf HandleMouseUp End Sub ' Clicking the TextBlock queues a delegate to perform a task on a ' thread pool thread. Private Shared Sub HandleMouseUp(ByVal sender As Object, _ ByVal e As System.Windows.Input.MouseButtonEventArgs) ThreadPool.QueueUserWorkItem(AddressOf AppendTextTask) ' Note: The stateInfo object for the task in this example is ' Nothing, because this overload of QueueUserWorkItem does not ' take a stateInfo parameter. End Sub ' This method performs the task, which is to append text to the ' TextBlock. To communicate with objects on the UI thread, get the ' Dispatcher for one of the UI objects. Use the Dispatcher object's ' BeginInvoke method to queue a delegate that will run on the UI thread, ' and therefore can safely access UI elements like the TextBlock. In ' this example, the delegate is stored in the field named 'append'. Private Shared Sub AppendTextTask(ByVal stateInfo As Object) outputBlock.Dispatcher.BeginInvoke(append, "Hello from the thread pool." & vbCrLf) End Sub ' Invoking this delegate runs the AppendText method. Private Shared append As New Action(Of String)(AddressOf AppendText) ' The AppendText method can safely access UI elements because it is run ' on the UI thread, by using the Dispatcher.BeginInvoke method. Private Shared Sub AppendText(ByVal text As String) outputBlock.Text &= text End Sub End Class ' This code produces output similar to the following: ' 'Click here to start a background task. 'Hello from the thread pool. 'Hello from the thread pool. 'Hello from the thread pool.
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.