I have an async method:
public async Task<bool> ValidateRequestAsync(string userName, string password) { using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(url); string stringResponse = await response.Content.ReadAsStringAsync(); return bool.Parse(stringResponse); } }
I call this method like this:
bool isValid = await ValidateRequestAsync("user1", "pass1");
Can i call the same method from an synchronous method, without using await
keyword?
Ex:
public bool ValidateRequest(string userName, string password) { return ValidateRequestAsync(userName, password).Result; }
I think this will cause a deadlock.
EDIT
Calling the method like above makes the call never end. (The method doesn't reaches the end anymore)
In this way, an async function without an await expression will run synchronously. If there is an await expression inside the function body, however, the async function will always complete asynchronously. Code after each await expression can be thought of as existing in a .then callback.
Consider Using async without await. think that maybe you misunderstand what async does. The warning is exactly right: if you mark your method async but don't use await anywhere, then your method won't be asynchronous. If you call it, all the code inside the method will execute synchronously.
Each await gets combined into a AsyncStateMachine that can process stuff asynchronously. If you don't have await keyword inside async method then there is no reason to create AsyncStateMachine , so you get synchronous method. await is used to wait for a result to arrive.
However, just to address "Call an async method in C# without await", you can execute the async method inside a Task. Run . This approach will wait until MyAsyncMethod finish. await asynchronously unwraps the Result of your task, whereas just using Result would block until the task had completed.
If you call an async method from a single threaded execution context, such as a UI thread, and wait for the result synchronously, there is a high probability for deadlock. In your example, that probability is 100%
Think about it. What happens when you call
ValidateRequestAsync(userName, password).Result
You call the method ValidateRequestAsync. In there you call ReadAsStringAsync. The result is that a task will be returned to the UI thread, with a continuation scheduled to continue executing on the UI thread when it becomes available. But of course, it will never become available, because it is waiting (blocked) for the task to finish. But the task can't finish, because it is waiting for the UI thread to become available. Deadlock.
There are ways to prevent this deadlock, but they are all a Bad Idea. Just for completeness sake, the following might work:
Task.Run(async () => await ValidateRequestAsync(userName, password)).Result;
This is a bad idea, because you still block your UI thread waiting and doing nothing useful.
So what is the solution then? Go async all the way. The original caller on the UI thread is probably some event handler, so make sure that is async.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With