Hi i am posting to a url using following method. it works fine for me. i have created five task inside this.
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());
}
}
what i need to achieve is if any task returns string containing "START" word stop waiting and continue ..
The way i am doing this is as follows :
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());
and so on waitng up to task 5. is there any way to do this code using Task.waitany please suggest
You could try using the solution given in this post
You will need to pass your condition to this method as a predicate.
Assuming lst_tasks contains all the tasks, you could do something like this.
await WhenAny(lst_tasks, s => s == "START");
This will stop waiting once any task returns START.