WinRT: Loading static data with GetFileFromApplicationUriAsync()

前端 未结 1 1906
轮回少年
轮回少年 2020-12-06 19:55

I have some data in my windows 8 application which should be shipped with and is just some static data. In fact: It\'s a simple xml file which should get deserialized.

相关标签:
1条回答
  • 2020-12-06 20:41

    Thanks for posting code. Please try to change your method Load as the following:

    //your code
    var file = await StorageFile.GetFileFromApplicationUriAsync(uri).AsTask().ConfigureAwait(false);
    //your code
    var stream = await file.OpenStreamForReadAsync().ConfigureAwait(false);
    //your code
    

    The main difference here is AsTask().ConfigureAwait(false)

    EDIT:

    Good to hear that it's working. Explanation is quite simple: when you use task.Result or task.Wait() on GUI thread in conjunction with await keyword you're causing deadlock. This happens because after awaiting code is resumed on the same context it was invoked (in your case - GUI thread). And because GUI thread currently waiting for task to complete (via Result or Wait()) deadlock arises and code after await keyword will never be invoked. ConfigureAwait(false) specifies that current context can be ignored and thus allows your code to complete successfully. More details on this here: http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

    0 讨论(0)
提交回复
热议问题