Thread Wont Go Away!

  • Thread starter Thread starter Matt
  • Start date Start date
M

Matt

Ok, this is probably a simple and stupid question, but its giving me hell.

Lets say that this is my app

public class myapp : System.Windows.Forms.Form
{
public myapp()
{
}

private void btnGo_Click(blah, blah)
{
ThreadStuff ts = new ThreadStuff();
}
}

public class ThreadStuff
{
Thread tt;
public ThreadStuff()
{
tt = new Thread(new ThreadStart(this.GoDoSomething));
tt.Start();
}
private void GoDoSomething()
{
while(true)
{
Thread.Sleep(new timespan(0,0,10);
}
}
}

I know the app does nothing, but stay with me:)

When I close the app the process is still running in task manager. What do
I need to do to clean up the thread and abort it? Shouldnt the instance of
ThreadStuff get disposed of and does that not kill all the objects including
the threat tt?

Obviously, Im doing something wrong. Let me know what the best way to kill
a thread that is running whenever the application is closed.

Thanks for the help.
 
Set the 'IsBackground' property of your thread to 'true' before you Start()
it.
 
Design your thread handling classes to do a clean shutdown. I posted an
example in the past. Do google groups search with this pattern to find
it. "Shutdown pattern lambertl". This was done for Java but it is nearly
the same as C#.

Hope this helps
Leon Lambert
 
Back
Top