I have the following code here
public static async Task<string> Start(IProgress<ProcessTaskAsyncExProgress> progress)
{
const int total = 10;
for (var i = 0; i <= total; i++)
{
await Task.Run(() => RunLongTask(i.ToString(CultureInfo.InvariantCulture)));
if (progress != null)
{
var args = new ProcessTaskAsyncExProgress
{
ProgressPercentage = (int)(i / (double)total * 100.0),
Text = "processing " + i
};
progress.Report(args);
}
}
return "Done";
}
private static string RunLongTask(string taskName)
{
Task.Delay(300);
return taskName + "Completed!";
}
In this line:
await Task.Run(() => RunLongTask(i.ToString(CultureInfo.InvariantCulture)));
How do I get back the string value of RunLongTask?
I've tried
var val = await Task.Run(() => RunLongTask(i.ToString(CultureInfo.InvariantCulture))).Result;
But I get an error "string is not awaitable"
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:
And then call that instead with .Result like below:
Remove the
Result
from the end. When youawait
you will get theResult
back from the await-able method.