Before reading this article, please go through the following articles-
- Developing Windows Store Apps: Introduction And Requirement Specification
- Plans For Designing Metro Style Apps (Windows Store Apps)
- Windows Store Application Life-Cycle
- Guidelines to Pass Windows Store App Certification
- Navigation Patterns to Develop Windows Store Application
- Templates to Develop Windows Store Apps
- Develop Your First Windows Store App (Hello World)
- Controls to Design Windows Store Apps
- Lay-out Design Guidelines For Windows Store Apps
- Guidelines to Create Splash Screen For Windows Store Application
- Configure Your Windows Store Application Using Manifest Designer
- Working With Progress Controls in Windows Store Application
- Global AppBar For Windows Store Applications
- Using Webcam to Preview Video In Windows Store App
- Rotate Captured Video in Windows Store App
- Async and Await in Windows Store Application
Introduction
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 the 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.
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.
Handling Work Item Completion
- ThreadPoolWorkItem.Completed = new AsyncActionCompletedHandler(
- (IAsyncAction source, AsyncStatus status) =>
- {
- Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High,
- () =>
- {
- // Your code to update UI });
- });

Join the conversation! Your thoughts help the community grow.