调用异步任务<T> 方法
Calling an Asyc Task<T> method
我现在正在学习异步 (UWP) 方法,但不太了解语法。所有示例都进行了很多事情,而不仅仅是任务异步调用,并且没有真正显示它们是如何启动的。我不明白的主要事情是如何从非异步方法启动任务。每次我尝试调用该任务时,它都希望我将该方法转换为异步方法。这反过来又希望我将调用该方法的任何方法转为异步。那么你应该如何开始呢?
我正在尝试的当前用例是从 UWP 本地存储中检索一个 JSON 包含用户名和 ID 的文件,现在在 UWP 中是一个异步调用。我很确定我的方法是正确的,但我在调用它时遇到问题。目前我在它支持的页面的代码后面调用该方法,实际方法当前位于用户 Class.
这是我的代码:
public async Task<List<User>> getUsers()
{
List<User> u = new List<User>();
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.GetFileAsync("LocalUsers.txt");
string Json = await FileIO.ReadTextAsync(file);
List<User> existingUsers = new List<User>();
existingUsers = JsonConvert.DeserializeObject<List<User>>(Json);
return existingUsers;
}
这就是我试图从
后面的代码中调用该方法的方式
List<User> users = await User.getUsers();
我也尝试过 运行 类似这样的任务,但我认为我的语法不正确或其他什么
Task t = new Task(User.getUsers());
如有任何帮助,我们将不胜感激。谢谢。
异步代码往往会感染整个代码库。您可以 运行 任务与 Task.Wait 同步。虽然不推荐这样做。您也可以使用 Task.Run 将其发送到后台线程,但是当然,您无法轻松检索任何结果。
The main thing I don't understand is how to initiate the task from a non async method. Everytime I try to call the task, it wants me to convert that method to an async one. Which in turn would want me to turn any method that calls that one to async. So how are you supposed to start it?
正如我在我的异步最佳实践文章中所描述的那样,async
does "grow" through the code base. This is natural and should be embraced. Especially on a platform such as UWP, where blocking on asynchronous code can easily cause deadlocks 并且会立即取消您在应用商店的资格。
在最简单的情况下,async
增长在异步事件处理程序处停止,即 async void
。如果您正在进行基于 MVVM 的开发,则还有其他注意事项;我在 async MVVM development.
上的三部分 MSDN 文章系列中介绍了这些内容
如果您有现有的代码库,您还可以找到我的 article on brownfield async helpful。
如果您不想使调用方法异步,则可以执行以下操作
List<User> users = User.getUsers().Result;
我现在正在学习异步 (UWP) 方法,但不太了解语法。所有示例都进行了很多事情,而不仅仅是任务异步调用,并且没有真正显示它们是如何启动的。我不明白的主要事情是如何从非异步方法启动任务。每次我尝试调用该任务时,它都希望我将该方法转换为异步方法。这反过来又希望我将调用该方法的任何方法转为异步。那么你应该如何开始呢?
我正在尝试的当前用例是从 UWP 本地存储中检索一个 JSON 包含用户名和 ID 的文件,现在在 UWP 中是一个异步调用。我很确定我的方法是正确的,但我在调用它时遇到问题。目前我在它支持的页面的代码后面调用该方法,实际方法当前位于用户 Class.
这是我的代码:
public async Task<List<User>> getUsers()
{
List<User> u = new List<User>();
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.GetFileAsync("LocalUsers.txt");
string Json = await FileIO.ReadTextAsync(file);
List<User> existingUsers = new List<User>();
existingUsers = JsonConvert.DeserializeObject<List<User>>(Json);
return existingUsers;
}
这就是我试图从
后面的代码中调用该方法的方式 List<User> users = await User.getUsers();
我也尝试过 运行 类似这样的任务,但我认为我的语法不正确或其他什么
Task t = new Task(User.getUsers());
如有任何帮助,我们将不胜感激。谢谢。
异步代码往往会感染整个代码库。您可以 运行 任务与 Task.Wait 同步。虽然不推荐这样做。您也可以使用 Task.Run 将其发送到后台线程,但是当然,您无法轻松检索任何结果。
The main thing I don't understand is how to initiate the task from a non async method. Everytime I try to call the task, it wants me to convert that method to an async one. Which in turn would want me to turn any method that calls that one to async. So how are you supposed to start it?
正如我在我的异步最佳实践文章中所描述的那样,async
does "grow" through the code base. This is natural and should be embraced. Especially on a platform such as UWP, where blocking on asynchronous code can easily cause deadlocks 并且会立即取消您在应用商店的资格。
在最简单的情况下,async
增长在异步事件处理程序处停止,即 async void
。如果您正在进行基于 MVVM 的开发,则还有其他注意事项;我在 async MVVM development.
如果您有现有的代码库,您还可以找到我的 article on brownfield async helpful。
如果您不想使调用方法异步,则可以执行以下操作
List<User> users = User.getUsers().Result;