Getting return value from Task.Run
Solution 1:
Remove the Result
from the end. When you await
you will get the Result
back from the await-able method.
var val = await Task.Run(() => RunLongTask(i.ToString(CultureInfo.InvariantCulture)));
Solution 2:
This is not a direct answer to old question, but for others searching:
"Normally" you shouldn't do this, but sometimes you need to match a library API so you can use a wrapper function like below:
private async Task<string> WrapSomeMethod(string someParam)
{
//adding .ConfigureAwait(false) may NOT be what you want but google it.
return await Task.Run(() => SomeObj.SomeMethodAsync(someParam)).ConfigureAwait(false);
}
And then call that instead with .Result like below:
string blah = WrapSomeMethod(someParam).Result;