使用await 与Result

max*_*max 2 c# async-await

有人可以解释一下异步模式中的usingawait和 the之间有什么区别,以及我将在哪里使用它们?Result

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
}
Run Code Online (Sandbox Code Playgroud)

Sie*_*jet 5

调用Result将阻塞当前线程,直到操作完成。当异步操作完成时,返回await给调用者并继续该方法的其余部分。

  • `将阻塞当前线程直到操作完成` - 或者,很多时候,将阻塞当前线程[永久](https://blog.stephencleary.com/2012/07/dont-block-on-async-code .html)。 (2认同)