Before reading this article, please go through the following articles:
IntroductionIn 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 poolIn 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 CompletionWhen 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 }); }); ConclusionUsing a thread pool in a Windows Store application, we can perform the async calls in parallel.
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: