I have some code that can spawn background worker threads from anywhere within it. This usually runs as a windows service.
I now need to run the same code from a win forms app (which will usually be run through visual studio rather than from a standalone exe).
When I run the main thread, it exits and returns control to the UI, however there may still be background threads running. Someone can then close the app/stop debugging and it terminates the other threads.
How can I prevent this (i realise i cant prevent it being terminated, i mean make the user aware so they dont terminate it) by making my app aware of all the child-threads that might have been spawned and wait for them to exit?
Is there a way to get the number of currently executing background threads and wait for this to be zero?
I’d rather not add code to the places where i spawn the new threads, because it isnt their concern.
Thanks
Although you’ll have to make a change to your threads and their launcher, one simple way I’ve used (if you’re creating your own threads) is to increment a counter as you launch each thread and use a callback method in each thread that decrements the counter.