问题描述
我有一个适用于Windows Store应用程序(aka Metro)的简单实用程序集合,但是我发现连续调用ApplicationData.Current.LocalFolder.GetFileAsync
时它似乎挂起了(即,当我尝试调试并逐步执行时)通过,问题消失了.)
I've got a simple utility collection for a Windows Store application (a.k.a Metro), but I'm finding that it seems to hang when calling ApplicationData.Current.LocalFolder.GetFileAsync
in succession (i.e. as soon as I try to debug it and step through, the problem disappears).
public static async Task<LocalCache<T>> LoadCache()
{
try
{
// Get the input stream for the cache
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(GetFileNameForType());
using (IInputStream inStream = await file.OpenSequentialReadAsync())
{
// Deserialize the cache
DataContractSerializer serializer = new DataContractSerializer(typeof(LocalCache<T>));
return (LocalCache<T>)serializer.ReadObject(inStream.AsStreamForRead());
}
}
catch (FileNotFoundException)
{
return new LocalCache<T>();
}
}
指定打开文件的模式似乎没有任何重载,并且似乎没有返回,这让我有些困惑.我该如何防止这种僵局?
There doesn't appear to be any overload to specify a mode to open the file in, and it doesn't appear to return, which has left me kinda stumped. What can I do prevent this deadlock?
推荐答案
最可能的原因是您的调用堆栈更深了.我猜想可能有对Wait
或Result
的调用.此将导致死锁(如我的博客).
The most likely cause of this is further up your call stack; I'm guessing there is probably a call to Wait
or Result
. This will result in deadlock (as described on my blog).
如果由于这是初始化或构造函数的一部分而被阻止,则您可能对异步延迟初始化(也在我的博客中进行了介绍).
If you're blocking because this is part of initialization or a constructor, then you may be interested in asynchronous lazy initialization (also described on my blog).
这篇关于什么会导致GetFileAsync挂起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!