当前位置:网站首页>From task Run get return value - getting return value from task Run

From task Run get return value - getting return value from task Run

2022-07-05 02:19:00 French red wine sweet

problem :

I have the following code: I have the following code :

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!";}

How do I get back the string value of RunLongTask from this line: await Task.Run(() => RunLongTask(i.ToString(CultureInfo.InvariantCulture))); How to start from this line RunLongTask The string value of : await Task.Run(() => RunLongTask(i.ToString(CultureInfo.InvariantCulture)));??

I've tried: I tried :

var val = await Task.Run(() => RunLongTask(i.ToString(CultureInfo.InvariantCulture))).Result;

But I get an error saying "string is not awaitable". But I received an error message , say “ String cannot wait ”.


Solution :

Reference resources : https://stackoom.com/en/question/1DjqW
原网站

版权声明
本文为[French red wine sweet]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140935048903.html