Unexpected deadlock (partially-async WinRT)

Bloqueada Unexpected deadlock (partially-async WinRT)

Todas las respuestas

  • miércoles, 21 de diciembre de 2011 7:48
     
     Respondida Tiene código

    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)

    • Propuesto como respuesta macrogreg miércoles, 21 de diciembre de 2011 7:49
    • Marcado como respuesta Stephen ClearyMVP miércoles, 21 de diciembre de 2011 19:40
    •  
  • viernes, 08 de junio de 2012 15:32
     
     

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

  • martes, 17 de julio de 2012 14:53
     
      Tiene código

    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.