Make sure all threads are closed or forcibly closed by thread?

I am a WPF / C # application that has a psuedo process Click the button> Start Stream to take a snapshot from the webcam API> Instant Webcam API> The API starts the callback thread> Shooting in progress> The API object is located

These steps usually work, except in the last part, where the webcam API callback thread does not close. Thus, in my task manager, I always get a ghost process that has the same name as my underlying WPF application. In addition, a second call to the same webcam API (to take a second snapshot) fails.

So, I am trying to find a way to make sure that all my threads from my root application are forcibly closed at any time. Is there any way to make sure that no threads are left?

+4
source share
3 answers

If you set IsBackground to true in these threads, they will be terminated at shutdown.

+8
source

If you have a window in your application, set Application.ShutdownMode to OnMainWindowClose. This will stop any current background threads for you when the main window closes. Programmatically, you can call Environment.Exit to kill all threads as gracefully as possible (but hard if not) and exit the application.

+3
source

If you see some kind of ghostly process besides your main program, this is not a thread problem. You have created another process to perform the operation. Kill it. Your resources will be released. You don’t have to worry about threads running when a big problem arises - another process is hanging with resources.

0
source

All Articles