c# - 异步 LoadImage 例程挂起

标签 c# microsoft-metro async-await loadimage

我有一个 Windows 8 应用程序,我尝试使用以下代码加载图像:

    private async Task<BitmapImage> LoadImage(IStorageFile storageFile)
    {
        System.Diagnostics.Debug.WriteLine("LoadImage started");

        try
        {
            // Set the image source to the selected bitmap
            BitmapImage bitmapImage = null;

            // Ensure a file was selected
            if (storageFile != null)
            {
                System.Diagnostics.Debug.WriteLine("LoadImage::OpenAsync");
                // Ensure the stream is disposed once the image is loaded
                using (IRandomAccessStream fileStream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
                {
                    System.Diagnostics.Debug.WriteLine("New Bitmap");
                    // Set the image source to the selected bitmap
                    bitmapImage = new BitmapImage();


                    System.Diagnostics.Debug.WriteLine("Set Source");
                    bitmapImage.SetSource(fileStream);
                }
            }

            return bitmapImage;
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        } // End of catch
        finally
        {
            System.Diagnostics.Debug.WriteLine("Load image finished");
        }

        return null;
    }

当我运行代码时,它有时会起作用。但其他时候它只是挂起,我得到以下输出:

LoadImage started
LoadImage::OpenAsync

Am I using storageFile.OpenAsAsync incorrectly? My storage file is the result of a call to:

        FileOpenPicker openPicker = new FileOpenPicker();
        openPicker.ViewMode = PickerViewMode.List;
        openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;

        openPicker.FileTypeFilter.Add(".jpg");
        openPicker.FileTypeFilter.Add(".png");
        openPicker.FileTypeFilter.Add(".bmp");

        StorageFile file = await openPicker.PickSingleFileAsync();
        if (file != null)
        {
          var loadImageTask = LoadImage(currentStorageFile);
          loadImageTask.Wait();
          DisplayImage.Source = loadImageTask.Result;
        }

所以这不应该是沙箱问题(也不异常(exception))。

有人能指出我正确的道路吗?

最佳答案

调用Task.WaitTask.Result正在造成僵局。我详细解释一下on my blogin a recent MSDN article .

简而言之,当您 await一个Task尚未完成,默认情况下它将捕获“上下文”并使用它来恢复 async方法。在您的例子中,这是 UI 上下文。但是,当您调用Wait时(或 Result ),您正在阻塞 UI 线程,因此 async方法无法完成。

要解决此问题,请使用 await而不是Wait/Result ,并使用ConfigureAwait(false)尽你所能。

关于c# - 异步 LoadImage 例程挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16250560/

相关文章:

c# - 为什么 MahApps.Metro ShowProgressAsync 对话框意外重绘? (总是灰色的)

c# - 如何通过接口(interface)方法使用 CallerMemberName

c# - 如何检测 RichTextBlock 在 Windows 应用商店应用程序中是否有溢出内容?

c# - 从 websocket 接收字节 - Windows 8 应用程序

c# - 在 Metro 应用程序中显示动画 GIF

.net - MVVM 在 Windows 8 应用商店应用程序中死了吗?

javascript - 并行异步迭代器 - 有可能吗?

c# - 带有可取消 EventArgs 的 INotifyPropertyChanging EventHandler 中的异步/等待

c# - 基于屏幕尺寸的水平与垂直导航栏

c# - [纯] 函数可以抛出异常吗?