GetFileAsync挂起 [英] GetFileAsync hangs
问题描述
我使用下面的代码,但不知何故,代码停在: 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的详细信息。
此处的最佳做法是:
有两种避免这种情况的最佳做法:
- In您的异步方法,尽可能使用ConfigureAwait(false)。
- 不要阻止任务;一直使用async。
祝你好运,
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.
And best practices here is:
There are two best practices that avoid this situation:
- In your async methods, use ConfigureAwait(false) wherever possible.
- Don’t block on Tasks; use async all the way down.
Best regards,
这篇关于GetFileAsync挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!