How to kill Thread on exit?

后端 未结 2 2051
Happy的楠姐
Happy的楠姐 2020-12-14 07:47

A button on the parent form is used to start the thread. If the parent form is closed in the development environment the thread keeps running in the background preventing ed

2条回答
  •  悲&欢浪女
    2020-12-14 08:04

    If you want the app to exit when the main thread has finished, you can just make the new thread a background thread:

    Thread t = new Thread(Main.MyThread);
    t.IsBackground = true;
    t.Start();
    

    Basically the process will exit when all the foreground threads have exited.

    Note that this could be bad news if the background thread is writing a file when the form is closed, or something similar...

提交回复
热议问题