有人可以解释一下使用await和异步模式中的结果有什么区别?我会在哪里使用?
public static async Task<string> GetVersion(int port, string method)
{
var client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:" + port);
return client.GetStringAsync("/test").Result; //<===============this versus
return await client.GetStringAsync("/test").ConfigureAwait(false);//<=====this
}
答案 0 :(得分:1)
调用Result
将阻止当前线程,直到操作完成并获得结果。 await
返回调用者并在异步操作完成时继续执行该方法的其余部分。