您好我正在使用以下方法发布到网址。这对我来说可以。我在这里创造了五个任务。
private async Task CreateMultipleTasksAsync(string url, ExtendedWebClient oExtendedWebClient, string sParam)
{
try
{
Task<string> download1 = oExtendedWebClient.ProcessURLAsync(url, oExtendedWebClient, sParam);
Task<string> download2 = oExtendedWebClient.ProcessURLAsync(url, oExtendedWebClient, sParam);
Task<string> download3 = oExtendedWebClient.ProcessURLAsync(url, oExtendedWebClient, sParam);
Task<string> download4 = oExtendedWebClient.ProcessURLAsync(url, oExtendedWebClient, sParam);
Task<string> download5 = oExtendedWebClient.ProcessURLAsync(url, oExtendedWebClient, sParam);
lst_tasks1.Add(download1);
lst_tasks2.Add(download2);
lst_tasks3.Add(download3);
lst_tasks4.Add(download4);
lst_tasks5.Add(download5);
// Await each task.
Result1 = await download1;
Result2 = await download2;
Result3 = await download3;
Result4 = await download4;
Result5 = await download5;
}
catch (Exception ex)
{
ErrorLog.createLog("ex.StackTrace = " + ex.StackTrace + " ex.tostring = " + ex.ToString());
}
}
我需要实现的是,如果任何任务返回包含&#34; START&#34;字停止等待并继续..
我这样做的方式如下:
Task.WaitAll(lst_tasks1.ToArray());
if (Result1.ToLower().Contains("START") && !Result1.Contains(sTextSearch))
{
goto call2;
}
else
{
worker.ReportProgress(0, "Waiting for tasks2");
Task.WaitAll(lst_tasks2.ToArray());
依旧等待任务5. 有没有办法使用Task.waitany来做这个代码请建议
答案 0 :(得分:1)
You could try using the solution given in this post
您需要将条件作为谓词传递给此方法。
假设lst_tasks包含所有任务,你可以这样做。
await WhenAny(lst_tasks, s => s == "START");
一旦任务返回START,这将停止等待。