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.
- 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...user2819245– user28192452018-11-12 17:33:50 +00:00Commented Nov 12, 2018 at 17:33
Add a comment |
2 Answers
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.