I'm using c# winforms to create an application. There is a thread that is activated by one of the user controls, and I want it to stop when the form is closed. So far I have managed to deal with it if the form is closed by a button inside the app, but not if it's being closed by the x of the actual form. How can I detect that the form is closed and then terminate the thread? thank you.
Asked
Active
Viewed 19 times
1
-
In the form’s Close event? – stuartd May 27 '21 at 20:25
-
Handle the FormClosed event. See this answer on another question: https://stackoverflow.com/a/2905947/8104777 – Caleb George May 27 '21 at 20:30
-
Note that if you set the thread's `IsBackground` property to `true`, then the thread will be terminated automatically when no other non-background threads are running. Note also that in the modern C# async/await idiom, there's almost never a good reason to create threads explicitly in the first place. – Peter Duniho May 27 '21 at 20:34