Thread Pool in Windows Store Application

Before reading this article, please go through the following articles:

  1. Developing Windows Store Apps : Introduction And Requirement Specification
  2. Plans For Designing Metro Style Apps (Windows Store Apps)
  3. Windows Store Application Life-Cycle
  4. Guidelines to Pass Windows Store App Certification
  5. Navigation Patterns to Develop Windows Store Application
  6. Templates to Develop Windows Store Apps
  7. Develop Your First Windows Store App (Hello World)
  8. Controls to Design Windows Store Apps
  9. Lay-out Design Guidelines For Windows Store Apps
  10. Guidelines to Create Splash Screen For Windows Store Application
  11. Configure Your Windows Store Application Using Manifest Designer
  12. Working With Progress Controls in Windows Store Application
  13. Global AppBar For Windows Store Applications
  14. Using Webcam to Preview Video In Windows Store App
  15. Rotate Captured Video in Windows Store App
  16. Async and Await in Windows Store Application

Introduction

In my last article we saw asynchronous programming in Windows Store applications. This article shows another way of doing asynchronous programming in a Windows Store application; using a "thread pool". In Windows Store applications the Windows runtime has one additional way to do asynchronous programming; using a thread pool. A thread pool is like async. If we need to call the work item or any code in parallel then we can use the thread pool provided in a Windows Store application.

What is a thread pool?

In Windows Store applications the thread pool works asynchronously to complete the task in parallel. The thread pool automatically manages the set of threads in the queue and assigns an appropriate work item or piece of code to the thread when the thread is free. Normally we use the async programming model to do the work without blocking the application. The UI thread pool is also similar to that; it won't block any UI element.

In Windows Store applications we have the thread management system provided by the Windows runtime using a thread pool. The thread pool is the secure way to manage the thread because we do not have the overhead of creating a thread and destroying those threads. The thread pool manages thread creation and destruction automatically. It is better to use a built-in thread pool instead of creating our own thread pool because the built-in thread pool is defined at the OS level.

Creating Work Item and Supplying to thread pool

In a Windows Store application you can create the work item using RunAsynch. This RunAsync returns an IAsyncAction object. If you need to update the UI then use CoreDispatcher.RunAsync. The following example creates the work item and assigns it to the thread pool.

IAsyncAction ThreadPoolWorkItem = Windows.System.Threading.ThreadPool.RunAsync(
    (source) =>
    {       
        // Your code. 

                  Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High,
                        () =>
                        {
                           
// update UI.
                        });
                }

            }

        }       
    });

In the above snippet when we call RunAsync the work item is created and has been queued to the thread pool. This work item executes when the thread is available.

Handling Work Item Completion

When you want to handle the work item completion and depending on it update the UI then you can use IAsyncAction.Completed. In async programming with a delegate we saw in the last article that we need a callback function to handle the completion like in IAsyncAction.Completed there. Have a look at the following snippet:

ThreadPoolWorkItem.Completed = new AsyncActionCompletedHandler(
    (IAsyncAction source, AsyncStatus status) =>
    {        
Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High,
            () =>
            { 
// Your code to update UI            });
    }); 

Conclusion


Using a thread pool in a Windows Store application, we can perform the async calls in parallel.

Up Next
    Ebook Download
    View all
    Learn
    View all