[英]async Task<bool> method call in if condition
I would like to know if the following code will wait for the async method to complete before executing the main thread or will just continue the main thread if condition and consider the method return as false.我想知道以下代码是否会在执行主线程之前等待异步方法完成,或者只是在有条件的情况下继续主线程并将方法返回为 false。
public async Task<bool> SomeMethod
{
if(await AsyncMethod(param))
{
//Do something
}
}
... ...
And the async method is defined as:异步方法定义为:
public async Task<bool> AsyncMethod(SomeClass param)
{
//Do something
}
I would like to know if the following code will wait for the async method to complete before executing the main thread or will just continue the main thread if condition and consider the method return as false.
我想知道以下代码是否会在执行主线程之前等待异步方法完成,或者只是在条件时继续主线程并将方法返回为false。
Neither. 都不是。
await
is an "asynchronous wait". await
是一种“异步等待”。 In other words, the method will wait, but the thread will not. 换句话说,该方法将等待,但线程不会。
When your method hits that await
(assuming it actually has some waiting to do), it will immediately return an incomplete task to the caller of SomeMethod
. 当你的方法遇到
await
(假设它实际上有一些等待做)时,它会立即将一个不完整的任务返回给SomeMethod
的调用者。 The thread continues with whatever it wants to do. 线程继续做它想做的事情。 Later on, when the task being awaited completes, then
SomeMethod
will resume executing. 稍后,当等待的任务完成时,
SomeMethod
将继续执行。 When SomeMethod
completes, the task it returned earlier will be completed. 当
SomeMethod
完成时,它先前返回的任务将完成。
I go into more detail on my blog post on the subject . 我在关于这个主题的博客文章中详细介绍了 。
It will wait for the operation to complete. 它将等待操作完成。
Note how you're invoking the operation: 请注意您是如何调用该操作的:
if(await AsyncMethod(param))
Two things: 两件事情:
await
keyword will, unsurprisingly, wait for the operation to complete. await
关键字将等待操作完成。 bool
can be used in a conditional, a Task<bool>
can not. bool
可以在条件中使用,但是Task<bool>
不能。 You can do it synchronous like this:您可以像这样同步进行:
if(AsyncMethod(param).Result)
{
//Do something
}
Hope it help;)希望它有所帮助;)
await
ing an async
method will cause the calling thread to return from the method caller until the async method completes. await
async
方法将导致调用线程从方法调用方返回,直到异步方法完成。 Once the method completes, the calling thread (in a synchronized context) will switch back to where it left off. 方法完成后,调用线程(在同步上下文中)将切换回停止的位置。 Without a synchronization context, the await method will still return back to the caller and execution of the calling method will resume (but not necessarily on the same thread it was originally called from).
如果没有同步上下文,await方法仍将返回给调用者,并且将恢复调用方法的执行(但不一定在最初调用它的同一线程上)。 This will happen regardless of the return type/value.
无论返回类型/值如何,都会发生这种情况。
In your case (assuming the syntax is correct). 在你的情况下(假设语法是正确的)。
SomeMethod
(you need to add ()) will call AsyncMethod
and return immediately. SomeMethod
(你需要add())将调用AsyncMethod
并立即返回。 Once AsyncMethod
is done, SomeMethod
will switch back to where it left off, which is evaluating the returned value ( true
or false
). AsyncMethod
完成后, SomeMethod
将切换回停止的位置,即评估返回值( true
或false
)。 If the value is true it will call //Do something, otherwise it will skip the if statement and finish executing SomeMethod
. 如果值为true,它将调用//执行某些操作,否则将跳过if语句并完成执行
SomeMethod
。
Here is an example of where there is no synchronization context. 以下是没有同步上下文的示例。 Notice the thread id changes after "await getStringTask", and the rest of GetValue is executed on the "new" thread.
注意线程id在“await getStringTask”之后发生了变化,其余的GetValue在“new”线程上执行。
private static void Main(string[] args)
{
GetValue();
Console.WriteLine("test1");
Console.WriteLine("1 " + Thread.CurrentThread.ManagedThreadId);
Console.ReadKey();
}
private static async void GetValue()
{
Console.WriteLine(await AccessTheWebAsync());
Console.WriteLine("2 " + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("test2");
Console.WriteLine("3 " + Thread.CurrentThread.ManagedThreadId);
}
private static async Task<int> AccessTheWebAsync()
{
HttpClient client = new HttpClient();
Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
Console.WriteLine("4 " + Thread.CurrentThread.ManagedThreadId);
string urlContents = await getStringTask;
Console.WriteLine("5 " + Thread.CurrentThread.ManagedThreadId);
return urlContents.Length;
}
Prints 打印
4 9
test1
1 9
5 13
47984
2 13
test2
3 13
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.