0

tell me how to write code as a result a callback function will be call to inform me that all task like task1,task2,task3 has been completed. thanks

enter image description here

Monojit Sarkar
  • 2,353
  • 8
  • 43
  • 94
  • Possible duplicate of [Proper way to use .ContinueWith for Tasks](http://stackoverflow.com/questions/11904821/proper-way-to-use-continuewith-for-tasks) – Igor Apr 25 '16 at 17:42
  • @Igor are you trying to say that i should use `ContinueWith()` ? basically i want to fire multiple function at a time but i need some callback function which fire when all function execution is completed just let me know job done. – Monojit Sarkar Apr 25 '16 at 17:47
  • That is exactly what `ContinueWith` is for. – Igor Apr 25 '16 at 17:50
  • i have 3 different instance of task then how could i use one ContinueWith for all 3 task instance? – Monojit Sarkar Apr 25 '16 at 18:01

1 Answers1

2

You can use Task.ContinueWith with Task.WhenAll or you can use Task.WaitAll with code to run following the call to WaitAll.

var executingTask = Task.WhenAll(task1, task2, task3).ContinueWith((antecedent) =>{/*your code*/});

See Task.ContinueWith documentation for additional details.

OR

// WaitAll blocks until all tasks are complete
Task.WaitAll(task1, task2, task3);
/*your code on the following lines(s) which will run after task1,task2,task3 are complete*/
Igor
  • 60,821
  • 10
  • 100
  • 175
  • what is antecedent ? – Monojit Sarkar Apr 25 '16 at 18:26
  • @MonojitSarkar - `antecedent` - `a thing or event that existed before or logically precedes another`. You get the completed task as argument that represented the execution of `task1`, `task2`, `task3`. – Igor Apr 25 '16 at 18:28
  • very sorry still not clear the usage of antecedent. can u explain it another way? – Monojit Sarkar Apr 25 '16 at 18:30
  • @MonojitSarkar - you get the completed task that represents the completion of the tasks passed to `Task.WhenAll`. Check out the documentation on `ContinueWith`, there is a code example that shows `Task.Factory.StartNew` that returns a `DateTime` array which is then accessed again in the `ContinueWith`. – Igor Apr 25 '16 at 18:35
  • why negative marks because i ask the question twice? – Monojit Sarkar Apr 25 '16 at 18:45