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 ?