Reputation: 151
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.
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
}
Upvotes: 11
Views: 20332
Reputation: 1194
You can do it synchronous like this :
if(AsyncMethod(param).Result)
{
//Do something
}
Hope it help ;)
Upvotes: -1
Reputation: 456407
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.
Neither.
await
is an "asynchronous wait". 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
. The thread continues with whatever it wants to do. Later on, when the task being awaited completes, then SomeMethod
will resume executing. When SomeMethod
completes, the task it returned earlier will be completed.
I go into more detail on my blog post on the subject.
Upvotes: 18
Reputation: 2390
await
ing an async
method will cause the calling thread to return from the method caller until the async method completes. 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).
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. Once AsyncMethod
is done, SomeMethod
will switch back to where it left off, which is evaluating the returned value (true
or false
). If the value is true it will call //Do something, otherwise it will skip the if statement and finish executing 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.
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
Upvotes: 1
Reputation: 218818
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.bool
can be used in a conditional, a Task<bool>
can not.Upvotes: 7