Unexpected deadlock (partially-async WinRT)

Locked Unexpected deadlock (partially-async WinRT)

所有回覆

  • 2011年12月21日 上午 07:48
     
     已答覆 包含代碼

    Hi there,

    You are hitting a known issue in the .NET developer preview version that Microsoft distributed at the BUILD conference.

    The issue is caused by some subtle specifics of the WinRT UI thread. As a result, any blocking WinRT stream-IO from managed code will lead to a deadlock, if performed from the UI thread. Asynchronous IO (e.g. ReadAsync) will work fine. The development team is aware of the problem and is working to fix it.

    Please note, however, that even if and when the issue is fixed, performing blocking IO on the UI thread is not a good idea. Your application will block and be unresponsive for the duration of the operation. Some .NET APIs do not have async equivalents (yet) and even once they do, converting code may require work. If you have to perform a blocking IO operation, make sure to offload it to the thread pool:

    DoUIStuff();
    Int32 x = await Task.Run(() => {
        OpenStream();
        PerformBlockingIO();
        ProcessResults();
        return ComputeIOResults();
    });
    UseIOResults(x);
    

    This way you keep your application responsive at all times. As a side effect, you will also work around the abovementioned bug and avoid the deadlock.

     

    Greg

    (.NET Base Class Libraries Team)

    • 已提議為解答 macrogreg 2011年12月21日 上午 07:49
    • 已標示為解答 Stephen ClearyMVP 2011年12月21日 下午 07:40
    •  
  • 2012年6月8日 下午 03:32
     
     

    Note this issue seems to be fixed in the Windows 8 Release Preview.

  • 2012年7月17日 下午 02:53
     
      包含代碼

    This type of WinRT IO-Stream still lead to deadlocks in the Windows 8 Release Preview.

    string fileContent = await FileIO.ReadTextAsync(storageFile);
    For example, the above code doesn't works for me and cause a deadlock. 

    However, answer given by macrogreg works fine, waiting a better answer.