Skip to main content Skip to footer

WinRT: Make Non-Asynchronous Code Asynchronous

If you’ve been working with WinRT and building Windows Store apps, then you probably have come across the await and async keywords. These .NET 4.5 keywords allow you to consume WinRT asynchronous operations. For a more full and detailed tutorial on asynchronous methods you should check out this great blog post from Microsoft, Exposing .NET Tasks as WinRT Asynchronous Operations. Async operations are critical in mobile apps that pull information from the Web. But they are also key when performing tasks that take longer than a couple seconds, for example, loading a large file from disk. Many WinRT APIs include Async methods such as the ReadTextAsync method below.


string s = await FileIO.ReadTextAsync(storageFile);  

This reads the contents from the text file into a string asynchronously. The app won’t hang or appear frozen in case we are loading a very large file. We also usually accompany async operatios with indeterminate progress bars or progress rings. Make Non Asynchronous Code Asynchronous But not everything is supported through async methods like above, especially things like UI controls. Below are two examples of how you can make non-asynchronous code into asynchronous code. The first example takes advantage of one of the WinRT Async Interfaces (they’re all described in the link above). In this case I’m using the IAsyncAction interface because I don’t have any results to return. Dispatcher is used to access our UI controls on another thread.


public IAsyncAction LoadSomethingAsync()  
{  
    return Task.Run(() =>  
    {  
        Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>  
            {  
                // do stuff to your UI  
            });  
    }).AsAsyncAction();  
}  

The code above is not as pretty, but it makes the method call very crisp and simple. Just use the await keyword before the call and it will now run asynchronously. You can test this by activating a progressRing before and it will continue to spin while the long operation continues.


progressRing.IsActive = true;  
await LoadSomethingAsync();  
progressRing.IsActive = false;  

The second approach is a bit fancier as it takes advantage of the ThreadPool class to queue our computationally-intensive work while not blocking the UI thread during its run. This time we use the async keyword on our method, which enables us to await the Dispatcher call.


public async void LoadSomething()  
{  
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>  
            {  
                // do stuff to your UI  
            });  
}  

This method is a bit cleaner than the first example, but now our code to call it is a bit longer.


progressRing.IsActive = true;  
await ThreadPool.RunAsync(delegate { LoadSomething(); });  
progressRing.IsActive = false;  

For more information on the ThreadPool, check out this blog post from Microsoft, Diving Deep with WinRT and await. Download a sample below which shows how to do some heavy data loading on a C1Chart control using the examples described in this post. Download ChartAsyncLoad Sample

ComponentOne Product Manager Greg Lutz

Greg Lutz

comments powered by Disqus