Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 edits to the source code on a 64 bit Windows 7 platform. The thread has to be killed by Menu > Debug > Stop Debugging. What is the proper way to programmatically kill the thread when the parent Form is closed?

private void buttonW_Click(object sender, EventArgs e)
{
    Thread t = new Thread(Main.MyThread);
    t.Start();
}

private static void MyThread()
{
    ...
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
338 views
Welcome To Ask or Share your Answers For Others

1 Answer

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...