Unexpected deadlock (partially-async WinRT)

Locked Unexpected deadlock (partially-async WinRT)

모든 응답

  • 2011년 12월 21일 수요일 오전 7: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일 수요일 오전 7:49
    • 답변으로 표시됨 Stephen ClearyMVP 2011년 12월 21일 수요일 오후 7:40
    •  
  • 2012년 6월 8일 금요일 오후 3:32
     
     

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

  • 2012년 7월 17일 화요일 오후 2: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.