Thread Wont Go Away!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt

    #1

    Thread Wont Go Away!

    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(bla h, blah)
    {
    ThreadStuff ts = new ThreadStuff();
    }
    }

    public class ThreadStuff
    {
    Thread tt;
    public ThreadStuff()
    {
    tt = new Thread(new ThreadStart(thi s.GoDoSomething ));
    tt.Start();
    }
    private void GoDoSomething()
    {
    while(true)
    {
    Thread.Sleep(ne w 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.


  • Shaniqua VonTheimenwhinselBergensteinSki

    #2
    Re: Thread Wont Go Away!

    Set the 'IsBackground' property of your thread to 'true' before you Start()
    it.


    "Matt" <everyone@email .com> wrote in message
    news:uN%23dyotr DHA.2492@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > 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(bla h, blah)
    > {
    > ThreadStuff ts = new ThreadStuff();
    > }
    > }
    >
    > public class ThreadStuff
    > {
    > Thread tt;
    > public ThreadStuff()
    > {
    > tt = new Thread(new ThreadStart(thi s.GoDoSomething ));
    > tt.Start();
    > }
    > private void GoDoSomething()
    > {
    > while(true)
    > {
    > Thread.Sleep(ne w 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[/color]
    do[color=blue]
    > I need to do to clean up the thread and abort it? Shouldnt the instance[/color]
    of[color=blue]
    > ThreadStuff get disposed of and does that not kill all the objects[/color]
    including[color=blue]
    > the threat tt?
    >
    > Obviously, Im doing something wrong. Let me know what the best way to[/color]
    kill[color=blue]
    > a thread that is running whenever the application is closed.
    >
    > Thanks for the help.
    >
    >[/color]


    Comment

    • Leon Lambert

      #3
      Re: Thread Wont Go Away!

      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


      Shaniqua VonTheimenwhins elBergensteinSk i wrote:[color=blue]
      > Set the 'IsBackground' property of your thread to 'true' before you Start()
      > it.
      >
      >
      > "Matt" <everyone@email .com> wrote in message
      > news:uN%23dyotr DHA.2492@TK2MSF TNGP12.phx.gbl. ..
      >[color=green]
      >>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(bla h, blah)
      >> {
      >> ThreadStuff ts = new ThreadStuff();
      >> }
      >>}
      >>
      >>public class ThreadStuff
      >>{
      >> Thread tt;
      >> public ThreadStuff()
      >> {
      >> tt = new Thread(new ThreadStart(thi s.GoDoSomething ));
      >> tt.Start();
      >> }
      >> private void GoDoSomething()
      >> {
      >> while(true)
      >> {
      >> Thread.Sleep(ne w 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[/color]
      >
      > do
      >[color=green]
      >>I need to do to clean up the thread and abort it? Shouldnt the instance[/color]
      >
      > of
      >[color=green]
      >>ThreadStuff get disposed of and does that not kill all the objects[/color]
      >
      > including
      >[color=green]
      >>the threat tt?
      >>
      >>Obviously, Im doing something wrong. Let me know what the best way to[/color]
      >
      > kill
      >[color=green]
      >>a thread that is running whenever the application is closed.
      >>
      >>Thanks for the help.
      >>
      >>[/color]
      >
      >
      >[/color]

      Comment

      Working...