Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get bool result from async task<bool> function in C# - Error: Cannot implicitly convert type `void' to `bool'

I have created task function for validating my json file. Everything works fine until I didn't use the result. When I am trying to get the result from async task<bool> function it is showing error as Cannot implicitly convert 'void' to bool. My async function is as follows:

 private async Task<bool> MyValidationFunction(string json)  {      bool isValid = true;      .......DOING MY VALIDATION STUFF.....      return isValid;  } 

Calling this function from another function is as follows:

 public bool GetJsonAndValidate()  {       bool isValid = true;       string jsonData = GetJson();       //******* Here I am getting the error.       bool isValid = MyValidationFunction(jsonData).Wait();   } 

When I am trying to call MyValidationFunction it is showing error as mention above. I have tried to get result by using Result property but it is throwing and error. My Class is just simple public class. I can do it with synchronous call but I need to have asynchronous call as MyValidationFunction get the result from database. If I didn't use the bool variable to capture the result, then it works fine. What I have missed out? How can I get bool result from my validation function?

like image 936
Iswar Avatar asked Jul 17 '17 07:07

Iswar


People also ask

How to return Value for Task bool?

To return Boolean from Task Synchronously, we can use Task. FromResult<TResult>(TResult) Method. This method creates a Task result that's completed successfully with the specified result. The calling method uses an await operator to suspend the caller's completion till called async method has finished successfully.

How to return Task in async method?

If you use a Task return type for an async method, a calling method can use an await operator to suspend the caller's completion until the called async method has finished. In the following example, the WaitAndApologizeAsync method doesn't contain a return statement, so the method returns a Task object.

How to return from async method C#?

The async method returning Task in C# Such type of async methods returns void if they run synchronously. If we have an async method with Task return type and if we want our caller method to wait until the async method completes its execution then we need to use the await operator while calling the async method.

What is the return type of async await?

The behavior of async / await is similar to combining generators and promises. Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise.


1 Answers

Statement 1. .Wait() has no return result. It is a void method, and therefore its result cannot be assigned to a variable.
You can use .Result which will wait until Task completes and return a result.

// Both are applicable to simple Tasks: bool isValid = MyValidationFunction(jsonData).Result;  // does that same as  var task = MyValidationFunction(jsonData); task.Wait();   bool isValid = task.Result; 

However, it is all valid for usual Tasks, but not for async/await functionality, because...

Statement 2. Do not mix up async and .Wait() - it still blocks the thread, killing the idea of async/await and negating all the performance improvement.

It also causes deadlock in WinForms, WPF, ASP.NET and other environments with SynchronizationContext. Read more about it in this Stephen Cleary's article or in these StackOverflow questions:

  • An async/await example that causes a deadlock
  • await works but calling task.Result hangs/deadlocks

Simple rule: if you use async, then you use await.

// That's how you do it with async/await: public async bool GetJsonAndValidate() {      string jsonData = GetJson();      bool isValid = await MyValidationFunction(jsonData);  } 

It will not block the thread and enable asynchronous behavior.

like image 61
Yeldar Kurmangaliyev Avatar answered Sep 24 '22 13:09

Yeldar Kurmangaliyev