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?
= await My...- Lei Yangasyncand.Wait()- it still blocks the thread and kills the idea of async/await. It also causes deadlock. If you useasync, then useawait. - Yeldar Kurmangaliyevasync, you'll tend to find that it (most naturally) propagates up the call stack. The most natural thing to do here would be to makeGetJsonAndValidateasyncalso, andawaitthe result from your validation function. - Damien_The_Unbeliever