我有以下代码:
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!";
}
如何RunLongTask
从这一行取回字符串值:await Task.Run(() => RunLongTask(i.ToString(CultureInfo.InvariantCulture)));
?
我试过了:
var val = await Task.Run(() => RunLongTask(i.ToString(CultureInfo.InvariantCulture))).Result;
但是我收到一条错误消息,说“字符串不可等待”。