I have one async method which return value and I want to add wait to this task.
var return = Task.Run(() => SomeMethod(param1)).Wait();
How can I get return value from above this line.
I have one async method which return value and I want to add wait to this task.
var return = Task.Run(() => SomeMethod(param1)).Wait();
How can I get return value from above this line.
2
Answers
The typical method would be to just write
This will block until the result becomes available. So it is equivalent to
Note that using
.Result
is generally not recommended. It will block the calling thread, so there is little point not just usingvar result = SomeMethod(param1)
. There is also the risk of deadlocks. If this is run on the UI thread, and SomeMethod uses.Invoke
or something else that waits for the UI thread, then your program will deadlock.The generally recommended method is to use async/await:
var result = await Task.Run(...)