Background Tasks in WinRT

1.9k views Asked by At

Ths situtation:

I get from different internet locations, json objects.

These containsmany Geocoordinates that I put onto a BingMap. That works perfect very well.

The Problem:

but when I fetch the data from the internet locations I get a blocking ui. Is there a method to run that in background?

I tried the async functionality but I get there a blocking UI too..

Here some code Caller

public async void Caller_Click(){
    await jsonDataClass.DoOperations();
}

The method in the jsonDataClass

public async Task<bool> DoOperations(){
    // do requests and some stuff..
    var fetchedElements = getdata(); // not async, because its in a portable lib
    foreach (var element in fetchedElements)
      OnEvent(element); // raises an event to assing the element to the Bing map
}
1

There are 1 answers

0
Stephen Cleary On

Don't ignore compiler warnings. In particular, if you have an async method that doesn't use await, then the compiler will specifically tell you that the method is not asynchronous and will run synchronously.

The first thing I would recommend is to change getdata so that it is async. The Microsoft.Bcl.Async NuGet package extends async support to portable class libraries.

If that's not possible, then you'll have to execute getdata on a background thread, like this:

public async Task<bool> DoOperations()
{
  var fetchedElements = await Task.Run(() => getdata());
  foreach (var element in fetchedElements)
    OnEvent(element);
}

P.S. The term "background task" has a special meaning in WinRT. What you actually need is to run code on a thread pool thread (or background thread), which is different than what WinRT calls a "background task".