I have a console app that runs a whole bunch of things via task.run
. I need all of those things to complete before the app terminates. The main line code just runs off the end of main. Will the process shutdown wait for all my task.runs
to complete or do I have to wire that up myself. If so does anybody have any suggestions.
Asked
Active
Viewed 1,498 times
1
-
Since tasks will run on background threads (i am ignoring other mechanisms to achieve concurrent/async behavior here), your program/process will not wait for them to finish before terminating. Rather, when your main thread terminates (more precisely, when the last foreground thread terminates), all still running background threads (your tasks) will simply be killed off unceremoniously... – Nov 12 '18 at 17:33
2 Answers
2
If you have an array of tasks, you can wait for all of them to complete using the
Task.WaitAll(tasks_array);

D-Shih
- 44,943
- 6
- 31
- 51

Ryan Pierce Williams
- 650
- 3
- 14
0
On a console app, the process shutdown will not wait for your tasks to finish. It calls ExitProcess, which effectively terminates all threads, including the thread pool that services the Tasks.
As @Ryan Pierce Willems wrote, you need to call Task.WaitAll to make sure you wait for all tasks to complete.

Nick
- 4,787
- 2
- 18
- 24