Windows 通用10个异步冻结程序
Windows universal 10 async freezes program
我想使用 FileIO 的 ReadBufferAsync 方法获取 BitmapImage。我要调用的方法是等待另一个异步方法的异步方法。
public class A
{
public static async Task<StorageFile> getFile(string fileName)
{
StorageFolder myfolder = ApplicationData.Current.LocalFolder;
return await myfolder.GetFileAsync(fileName);
}
public static async Task<BitmapImage> getImage(string fileName)
{
StorageFile file = getFile(fileName).Result;
var image = await FileIO.ReadBufferAsync(file);
return new BitmapImage(new Uri(file.Path));
}
}
在另一个class我叫
BitmapImage f = A.getImage("123.png").Result;
但是我的程序在等待 myfolder.GetFileAsync(fileName) 时死机了;
我认为这是因为它没有返回到 GUI 线程引起的。如果是这种情况,如何轻松转到 GUI 线程并返回此方法?
有解决这个问题的简单方法吗?
我尝试使用 Task.Run 和工厂
Task.Factory.StartNew(() =>
{
//code
});
提前致谢。
in an other class I call BitmapImage f = A.getImage("123.png").Result;
这是 UI 可能冻结的点。
请求任务的结果等待任务完成。在等待期间没有其他事情发生;这包括更新 UI。如果那不是你想要的,不要问结果; await
结果:BitmapImage f = await A.getImage("123.png");
。这意味着 "resume at this point when the image is available; keep processing the UI updates while we're waiting."
这是因为您正在使用 Result
属性 获取每个任务的结果。根据MSDN:
Accessing the property's get accessor blocks the calling thread until the asynchronous operation is complete; it is equivalent to calling the Wait method.
要解决此问题(或异步获取结果),您应该 await
结果:
public static async Task<BitmapImage> getImage(string fileName)
{
StorageFile file = await getFile(fileName);
var image = await FileIO.ReadBufferAsync(file);
return new BitmapImage(new Uri(file.Path));
}
在您告诉我们的其他 class 中,您应该:
BitmapImage f = await A.getImage("123.png");
我想使用 FileIO 的 ReadBufferAsync 方法获取 BitmapImage。我要调用的方法是等待另一个异步方法的异步方法。
public class A
{
public static async Task<StorageFile> getFile(string fileName)
{
StorageFolder myfolder = ApplicationData.Current.LocalFolder;
return await myfolder.GetFileAsync(fileName);
}
public static async Task<BitmapImage> getImage(string fileName)
{
StorageFile file = getFile(fileName).Result;
var image = await FileIO.ReadBufferAsync(file);
return new BitmapImage(new Uri(file.Path));
}
}
在另一个class我叫
BitmapImage f = A.getImage("123.png").Result;
但是我的程序在等待 myfolder.GetFileAsync(fileName) 时死机了; 我认为这是因为它没有返回到 GUI 线程引起的。如果是这种情况,如何轻松转到 GUI 线程并返回此方法?
有解决这个问题的简单方法吗?
我尝试使用 Task.Run 和工厂
Task.Factory.StartNew(() =>
{
//code
});
提前致谢。
in an other class I call
BitmapImage f = A.getImage("123.png").Result;
这是 UI 可能冻结的点。
请求任务的结果等待任务完成。在等待期间没有其他事情发生;这包括更新 UI。如果那不是你想要的,不要问结果; await
结果:BitmapImage f = await A.getImage("123.png");
。这意味着 "resume at this point when the image is available; keep processing the UI updates while we're waiting."
这是因为您正在使用 Result
属性 获取每个任务的结果。根据MSDN:
Accessing the property's get accessor blocks the calling thread until the asynchronous operation is complete; it is equivalent to calling the Wait method.
要解决此问题(或异步获取结果),您应该 await
结果:
public static async Task<BitmapImage> getImage(string fileName)
{
StorageFile file = await getFile(fileName);
var image = await FileIO.ReadBufferAsync(file);
return new BitmapImage(new Uri(file.Path));
}
在您告诉我们的其他 class 中,您应该:
BitmapImage f = await A.getImage("123.png");