Поделиться через


How to create and use pre-allocated work items (XAML)

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

Learn how to guarantee the availability of a work item by allocating resources for it ahead of time.

Sometimes work items accomplish critical work, but resource allocation isn't always guaranteed (for example when memory pressure occurs). You can guarantee the availability of a work item by using the PreallocatedWorkItem class to create it in advance. Once the PreallocatedWorkItem is created, the work item it represents is "on standby" and ready to be submitted to the thread pool.

Technologies

Instructions

Step 1: Create the PreallocatedWorkItem

Create a new PreallocatedWorkItem object and supply a lambda to do the work. Once it is created, you have a guarantee that the PreallocatedWorkItem can be submitted to the thread pool.

The following example creates a PreallocatedWorkItem in a constructor. Since the work item represents a critical task, this example uses a try-catch block to check whether the work item was successfully created:

private PreallocatedWorkItem ReadyWorkItem;

public ExamplePage()
    {
        this.InitializeComponent();

        try {

            ReadyWorkItem = new PreallocatedWorkItem((source) =>
                {
                    // 
                    // TODO: Critical task
                    // 
                    
                    // 
                    // Update the UI thread by using the UI core dispatcher.
                    // 
                    Dispatcher.RunAsync(CoreDispatcherPriority.High,
                        () =>
                        {
                            // 
                            // If appropriate, you can still access the
                            // UI thread from a pre-allocated work item.
                            //

                            // Work completed successfully.

                        });
                });

        } catch (Exception ex) {

            Debug.WriteLine("Pre-allocated work item could not be created.");
        }
    }
ExamplePage::ExamplePage()
{
    InitializeComponent();

    try {

        ReadyWorkItem1 = ref new PreallocatedWorkItem(
            ref new WorkItemHandler([this](IAsyncAction ^ operation)
            {
                // 
                // TODO: Critical task
                // 
                    
                // 
                // Update the UI thread by using the UI core dispatcher.
                // 
                Dispatcher->RunAsync(CoreDispatcherPriority::High,
                    ref new Windows::UI::Core::DispatchedHandler([this]()
                    {
                        // 
                        // If appropriate, you can still access the UI thread
                        // from a pre-allocated work item.
                        //

                        // Work completed successfully.

                    }));
            }));

    } catch (Exception^ ex) {

         OutputDebugString(L"Pre-allocated work item could not be created.");
    }
}

Step 2: Submit the PreallocatedWorkItem

To submit the work item, call the RunAsync method on the PreallocatedWorkItem.

Note  You can only submit a PreallocatedWorkItem once. Subsequent attempts to call RunAsync will throw an InvalidOperationException.

 

The following example submits the PreallocatedWorkItem from a destructor. It checks for an exception in case the work item was already submitted:

public ~ExamplePage ()
{
    if (ReadyWorkItem != null)
    {
        try
        {
            ReadyWorkItem.RunAsync();
        }
        catch (InvalidOperationException ex)
        {
            Debug.WriteLine("Pre-allocated work item was already submitted.");
        }
    }
}
ExamplePage::~ExamplePage()
{
    if (ReadyWorkItem1)
    {
        try
        {
            ReadyWorkItem1->RunAsync();
        }
        catch (Platform::Exception ^ ex)
        {
            OutputDebugString(L"Pre-allocated work item was already submitted.");
        }
    }
}

Quickstart: Submitting a work item to the thread pool

How to submit a work item using a timer

How to create a periodic work item

How to respond to named events and semaphores

How to use functions as work item delegates

Best practices for using the thread pool