0

I have a DTO object call ConvertionDto which has some string and Boolean fields.

I created a list of tasks as bellow

List<Task<Task<ConvertionDto>>> downloadTasksQuery = new 
                                              List<Task<Task<ConvertionDto>>>();

Then starting the task as bellow and passing the cancellation token as well

downloadTasksQuery.Add(Task.Factory.StartNew(() => 
                                    wcfClient.GetTimeEstimation(deleayTime), 
                                                                cancellationToken));
downloadTasksQuery.Add(Task.Factory.StartNew(() =>                                                
                                    wcfClient.DoConvertion("Hello"), 
                                                           cancellationToken)); 

Then I am running through the while loop and removing the completed tasks.

while (downloadTasksQuery.Count > 0)
{
  // Identify the first task that completes.  
  Task<Task<ConvertionDto>> firstFinishedTask = await Task.WhenAny(downloadTasks);                

  //process it more than once.  
  downloadTasks.Remove(firstFinishedTask);  
}

I want to terminate or cancel some tasks from the running current tasks when certain condition matches. I tried to passed the cancellation token as bellow, but nothing helped me.

cancellationTokenSource.cancel()

Is there any better way to handle this ?

MickyD
  • 14,343
  • 6
  • 43
  • 67
Shanjee
  • 459
  • 5
  • 14
  • @MickyD - Thanks for the help, I checked the mentioned linked task. yes its not possible for the network application, since the request sent to server we cannot cancel it. – Shanjee Jan 19 '18 at 04:37
  • Not a problem good sir. Wishing you well – MickyD Jan 19 '18 at 04:54

0 Answers0