简体   繁体   中英

Background Tasks in WinRT

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
}

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);
}

PS 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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM