Thursday, June 15, 2017

wait for async method to complete in c#?

we often gets the requirement for making asynchronous calls to synchronous due to various reasons here is the best solution to wait for asyncmethod to complete Solution is to run your method in task and get the result out of it


var result = Task.Run(async() => { return await yourAsyncMethod(); }).Result;


 This is best solution for wait until your async call complete

 There are other solutions which you can try

 YourAsyncMethod().GetAwaiter().GetResult();
 or
YourAsyncMethod().Result;

No comments:

Post a Comment