Exportar (0) Imprimir
Expandir todo
Este tema aún no ha recibido ninguna valoración - Valorar este tema

ThreadPool.QueueUserWorkItem Method (WaitCallback, Object)

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.

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)
[SecuritySafeCriticalAttribute]
public static bool QueueUserWorkItem(
	WaitCallback callBack,
	Object state
)

Parameters

callBack
Type: System.Threading.WaitCallback
The method to execute.
state
Type: System.Object
An object that contains data to be used by the method.

Return Value

Type: System.Boolean
true if the method is successfully queued; NotSupportedException is thrown if the work item could not be queued.
ExceptionCondition
NotSupportedException

The common language runtime (CLR) is hosted, and the host does not support this action.

ArgumentNullException

callBack is null.

If the callback method requires complex data, you can define a class to contain the data.

NoteNote:

Visual Basic and C# users can omit the WaitCallback delegate constructor when specifying the callback method in calls to the QueueUserWorkItem method. In Visual Basic, use the AddressOf operator. The compilers automatically call the correct delegate constructor.

The following example shows how to create an object that contains task information. It also demonstrates how to pass that object to a task that is queued for execution by the thread pool.

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.


using System;
using System.Threading;

class Example
{
    // This is the UI element that receives the output from the example.
    private static System.Windows.Controls.TextBlock outputBlock;

    // 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 static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        Example.outputBlock = outputBlock;
        outputBlock.Text = "Click here to start a background task.\r\n";

        // Set up an event handler to start a task when the TextBlock 
        // is clicked.
        outputBlock.MouseLeftButtonUp += HandleMouseUp;
    }

    // Clicking the TextBlock queues a delegate to perform a task on a 
    // thread pool thread. The data passed to the task is an object 
    // representing the time the task was queued.
    private static void HandleMouseUp(object sender, 
                                      System.Windows.Input.MouseButtonEventArgs e)
    {
        ThreadPool.QueueUserWorkItem(AppendTextTask, DateTime.Now);
    }

    // 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.
    private static void AppendTextTask(object stateInfo)
    {
        outputBlock.Dispatcher.BeginInvoke(delegate () {
            outputBlock.Text += String.Format(
                "Hello from the thread pool. Task queued at: {0}\r\n",
                stateInfo);
        });
    }
}

/* This code produces output similar to the following:

Click here to start a background task.
Hello from the thread pool. Task queued at: 6/30/2008 4:41:54 PM
Hello from the thread pool. Task queued at: 6/30/2008 4:42:03 PM
Hello from the thread pool. Task queued at: 6/30/2008 4:42:03 PM
 */


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

¿Te ha resultado útil?
(Caracteres restantes: 1500)
Gracias por sus comentarios

Adiciones de comunidad

AGREGAR
Mostrar:
© 2014 Microsoft. Reservados todos los derechos.