Search code examples
c#multithreadingwindows-phone-8

Calling main thread from a task in Windows phone 8.1


I have a code

async Task<String> RunAsync()
{
     using(var client = new HttpClient())
    {
         // Doing some Work without blocking UI.
         // Here i want to call my main thread delegate function
         del(jsonString);
    }
}

I want to call the delegate function in main thread. I tried Dispatcher but there is no Dispatcher Class. How would i do that in windows phone 8.1 sdk


Solution

  • You can access the Dispatcher of the CoreWindows from anywhere in your application like so:

    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () => {
    
        //call main thread here
    });
    

    Keep in mind, that calling the main thread takes some time (up to half a second on my Nokia Lumia 635). So if for instance if you have loops, do not use Dispatcher.RunAsync() in each iteration.