FileIO.ReadTextAsync occasionally hangs

后端 未结 2 2018
梦谈多话
梦谈多话 2021-01-23 04:58

I\'m just experimenting with WinRT and one demo app i\'m creating is a basic \"notepad\" style app which loads/saves to local storage. Whilst I\'m familiar with the proper

2条回答
  •  醉话见心
    2021-01-23 05:09

    Whilst I'm familiar with the proper async approach for building WinRT apps, my demo app is using a synchronous Load to keep things simple.

    Not really. Mixing synchronous with asynchronous code is extremely complex. It's far simpler to just use async everywhere.

    When an async method continues execution after waiting for a task, it will return to its original context by default. (I cover this in more detail in my async/await blog post). Some contexts (such as UI contexts) only permit a single thread; if that thread is blocked (e.g., on Task.Result), then the async method cannot enter that context to complete its execution. This causes a deadlock.

    For more information:

    • The async/await FAQ has a lot of detail on the context capture and resume.
    • Stephen Toub on the Parallel Team Blog has another blog post Await, and UI, and deadlocks! Oh, my!, which explains this particular deadlock situation in detail.
    • I wrote an exhaustive answer for this kind of deadlock in an MSDN forum post.

    This deadlock is famous enough that it's actually been demo'd by Microsoft:

    • By Stephen Toub at the BUILD conference (Sept 16, 2011).
    • By Lucian Wischik at DevConnections (Mar 26, 2012).

提交回复
热议问题