Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Thread Pool in Windows Store Application
WhatsApp
Krishna Garad
5y
26.3k
0
1
100
Article
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
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 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.
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.
Asynch using thread pool
Thread Pool
Thread Pool in windows store
Windows Store Application
Up Next
Ebook Download
View all
Printing in C# Made Easy
Read by 22.3k people
Download Now!
Learn
View all
Membership not found