GetFileAsync挂起 [英] GetFileAsync hangs

查看:90
本文介绍了GetFileAsync挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码,但不知何故,代码停在:  StorageFile fileToRead = await storageFolder.GetFileAsync(" cameras.xml");


在某些论坛上,据说您必须将其调整为:  StorageFile fileToRead = await storageFolder.GetFileAsync ("cameras.xml")。ConfigureAwait(False);


但是有一个错误消息:  IAsyncOperation不包含ConfigureAwait的定义


有人知道解决方案吗?


这是我用过的任务代码:

 public static async System.Threading.Tasks.Task< List< Camera>> LoadCameraXmlAsync()

{
//从xml文件加载相机列表
XDocument xmlDoc = new XDocument();
列表<相机> cameraList = new List< Camera>();
Windows.Storage.StorageFolder storageFolder =
Windows.Storage.ApplicationData.Current.LocalFolder;
bool fileExists = File.Exists(storageFolder.Path +" \\cameras.xml");
if(fileExists)
{
StorageFile fileToRead = await storageFolder.GetFileAsync(" cameras.xml")。ConfigureAwait(false);
using(var stream = await fileToRead.OpenStreamForReadAsync())
{
xmlDoc = XDocument.Load(stream);
stream.Flush();
}

使用(var reader = xmlDoc.CreateReader())
{
//从作者读取xml
var serializer = new XmlSerializer(的typeof(摄像机));
cameraList =(List< Camera>)serializer.Deserialize(reader);
}
}
返回cameraList;
}

解决方案

您好,


您是如何从代码中调用LoadCameraXmlAsync的?您是否尝试过以下代码:

 StorageFile fileToRead = await storageFolder.GetFileAsync(" cameras.xml")。AsTask()。ConfigureAwait(false); 

以下是有关为何使用ConfigureAwait的详细信息。


https://stackoverflow.com/questions/11316438/call-to-await-getfileasync-never-returns-and-app-hangs-in-winrt-app


此处的最佳做法是:


有两种避免这种情况的最佳做法:


  1. In您的异步方法,尽可能使用ConfigureAwait(false)。
  2. 不要阻止任务;一直使用async。

祝你好运,


Barry


I use the code below, but somehow the code stops at: StorageFile fileToRead = await storageFolder.GetFileAsync("cameras.xml");

On some forums it is said that you have to adjust it to: StorageFile fileToRead = await storageFolder.GetFileAsync("cameras.xml").ConfigureAwait(False);

But then there is an errormessage: IAsyncOperation does not contain a definition for ConfigureAwait

Does somebody knows a solution?

This is my used code for the task:

public static async System.Threading.Tasks.Task<List<Camera>> LoadCameraXmlAsync()

        {
            //load list of camera's from xml file
            XDocument xmlDoc = new XDocument();
            List<Camera> cameraList = new List<Camera>();
            Windows.Storage.StorageFolder storageFolder =
                Windows.Storage.ApplicationData.Current.LocalFolder;
            bool fileExists = File.Exists(storageFolder.Path + "\\cameras.xml");
            if (fileExists)
                {
                    StorageFile fileToRead = await storageFolder.GetFileAsync("cameras.xml").ConfigureAwait(false);
                    using (var stream = await fileToRead.OpenStreamForReadAsync())
                    {
                        xmlDoc = XDocument.Load(stream);
                        stream.Flush();
                    }

                    using (var reader = xmlDoc.CreateReader())
                    {
                        // read xml from the writer
                        var serializer = new XmlSerializer(typeof(Camera));
                        cameraList = (List<Camera>) serializer.Deserialize(reader);
                    }
                }
            return cameraList;
        }

解决方案

Hello,

How did you call LoadCameraXmlAsync from your code? Have you tried the following code:

 StorageFile fileToRead = await storageFolder.GetFileAsync("cameras.xml").AsTask().ConfigureAwait(false);

Here is the detail about why ConfigureAwait is used here.

https://stackoverflow.com/questions/11316438/call-to-await-getfileasync-never-returns-and-app-hangs-in-winrt-app

And best practices here is:

There are two best practices that avoid this situation:

  1. In your async methods, use ConfigureAwait(false) wherever possible.
  2. Don’t block on Tasks; use async all the way down.

Best regards,

Barry


这篇关于GetFileAsync挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆