Terminating thread

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

    #1

    Terminating thread

    I have a class that runs one of it's method in another thread. I use Thread
    object to do this and inside ThreadMethod I have an infinite loop:

    While (true)
    {
    // do something
    Thread.Sleep(10 0);
    }

    The problem is that I don't know how to terminate the thread when my class
    is destroyed. When closing application, destructor of my class is NOT called
    so I can't stop thread there. Looks like destructor will be called AFTER the
    thread has finished. Interesting and I'm stuck here because I don't know
    where to put the code that stops the thread. Also, I want my class to stop
    thread automatically upon destruction and I don't want to call some method
    of my class from outside to stop the thread.

    I can't believe that I can't solve this problem because doing it in Delphi
    it's a child's play :).

    regards
    Tomaz


  • Javaman59

    #2
    RE: Terminating thread

    Hi Tomaz,

    I am not sure whether your problem is as complicated as it sounds.

    Perhaps your problem is just that your thread won't terminate when the
    application terminates. If this is the case, then you need the IsBackground
    property...

    Thread myThread = new Thread(...);
    myThread.IsBack ground = true;
    myThread.Start( );

    Now your thread will terminate when the application terminates.

    However, perhaps your problem is more complicated...

    - You have a class which creates a thread
    - Your application creates an instance of the class
    - Your application deletes the instance
    - Your application continues running
    - You thread also continues running. Problem! You expect the thread to have
    been stopped.

    I have done some prototyping, and found that I cannot solve this problem. I
    have created a finalizer for the class, which stops the thread, as follows:

    class ThreadClass
    {
    ....

    volatile bool run = false;

    ~ThreadClass()
    {
    run = false;
    }

    void ThreadEntry()
    {
    run = true;
    while (run)
    {
    Thread.Sleep(30 0);
    }
    }

    }

    However, I have been unable to force the finalizer to be called. I have
    tried to make the instance available for collection, by setting it to null,
    and then forcing garbage collection with GC.Collect(), but this hasn't
    worked. The problem here is not terminating the thread, but getting the
    finalizer to be called.

    I suggest that you stop the thread from outside, by making a public method
    on the thread class...

    class ThreadClass
    {
    .....
    void Stop()
    {
    run = false;
    }
    }

    HTH,

    Javaman





    "Tomaz Koritnik" wrote:
    [color=blue]
    > I have a class that runs one of it's method in another thread. I use Thread
    > object to do this and inside ThreadMethod I have an infinite loop:
    >
    > While (true)
    > {
    > // do something
    > Thread.Sleep(10 0);
    > }
    >
    > The problem is that I don't know how to terminate the thread when my class
    > is destroyed. When closing application, destructor of my class is NOT called
    > so I can't stop thread there. Looks like destructor will be called AFTER the
    > thread has finished. Interesting and I'm stuck here because I don't know
    > where to put the code that stops the thread. Also, I want my class to stop
    > thread automatically upon destruction and I don't want to call some method
    > of my class from outside to stop the thread.
    >
    > I can't believe that I can't solve this problem because doing it in Delphi
    > it's a child's play :).
    >
    > regards
    > Tomaz
    >
    >
    >[/color]

    Comment

    • Tomaz Koritnik

      #3
      Re: Terminating thread

      Your example shows exactly the same problem I have. I solved it using
      IsBackground = true (hope this doesn't bring other bugs) but still I'm
      curious how to stop thread without "sending it to background". I don't want
      to declare public method because I want my class to control thread life. If
      at least there would exists some flag somewhere (in application or in thread
      itself) that will be set before app terminates.

      Thanks for trying it out.

      regards
      Tomaz


      "Javaman59" <Javaman59@disc ussions.microso ft.com> wrote in message
      news:610F282E-3766-4824-831A-A85C0541A977@mi crosoft.com...[color=blue]
      > Hi Tomaz,
      >
      > I am not sure whether your problem is as complicated as it sounds.
      >
      > Perhaps your problem is just that your thread won't terminate when the
      > application terminates. If this is the case, then you need the
      > IsBackground
      > property...
      >
      > Thread myThread = new Thread(...);
      > myThread.IsBack ground = true;
      > myThread.Start( );
      >
      > Now your thread will terminate when the application terminates.
      >
      > However, perhaps your problem is more complicated...
      >
      > - You have a class which creates a thread
      > - Your application creates an instance of the class
      > - Your application deletes the instance
      > - Your application continues running
      > - You thread also continues running. Problem! You expect the thread to
      > have
      > been stopped.
      >
      > I have done some prototyping, and found that I cannot solve this problem.
      > I
      > have created a finalizer for the class, which stops the thread, as
      > follows:
      >
      > class ThreadClass
      > {
      > ...
      >
      > volatile bool run = false;
      >
      > ~ThreadClass()
      > {
      > run = false;
      > }
      >
      > void ThreadEntry()
      > {
      > run = true;
      > while (run)
      > {
      > Thread.Sleep(30 0);
      > }
      > }
      >
      > }
      >
      > However, I have been unable to force the finalizer to be called. I have
      > tried to make the instance available for collection, by setting it to
      > null,
      > and then forcing garbage collection with GC.Collect(), but this hasn't
      > worked. The problem here is not terminating the thread, but getting the
      > finalizer to be called.
      >
      > I suggest that you stop the thread from outside, by making a public method
      > on the thread class...
      >
      > class ThreadClass
      > {
      > ....
      > void Stop()
      > {
      > run = false;
      > }
      > }
      >
      > HTH,
      >
      > Javaman
      >
      >
      >
      >
      >
      > "Tomaz Koritnik" wrote:
      >[color=green]
      >> I have a class that runs one of it's method in another thread. I use
      >> Thread
      >> object to do this and inside ThreadMethod I have an infinite loop:
      >>
      >> While (true)
      >> {
      >> // do something
      >> Thread.Sleep(10 0);
      >> }
      >>
      >> The problem is that I don't know how to terminate the thread when my
      >> class
      >> is destroyed. When closing application, destructor of my class is NOT
      >> called
      >> so I can't stop thread there. Looks like destructor will be called AFTER
      >> the
      >> thread has finished. Interesting and I'm stuck here because I don't know
      >> where to put the code that stops the thread. Also, I want my class to
      >> stop
      >> thread automatically upon destruction and I don't want to call some
      >> method
      >> of my class from outside to stop the thread.
      >>
      >> I can't believe that I can't solve this problem because doing it in
      >> Delphi
      >> it's a child's play :).
      >>
      >> regards
      >> Tomaz
      >>
      >>
      >>[/color]
      >[/color]


      Comment

      • Ron

        #4
        Re: Terminating thread

        You are going to need to send a message to the class so that it stops the
        thread, which means you will need to call a method outside your class, like
        Dispose (if you don't want the background thread). The destructor will not
        get called if a thread started from that class is still running, and your
        application will wait for that thread to finish too. I'm afraid those are the
        rules of this game.

        "Tomaz Koritnik" wrote:
        [color=blue]
        > Your example shows exactly the same problem I have. I solved it using
        > IsBackground = true (hope this doesn't bring other bugs) but still I'm
        > curious how to stop thread without "sending it to background". I don't want
        > to declare public method because I want my class to control thread life. If
        > at least there would exists some flag somewhere (in application or in thread
        > itself) that will be set before app terminates.
        >
        > Thanks for trying it out.
        >
        > regards
        > Tomaz
        >
        >
        > "Javaman59" <Javaman59@disc ussions.microso ft.com> wrote in message
        > news:610F282E-3766-4824-831A-A85C0541A977@mi crosoft.com...[color=green]
        > > Hi Tomaz,
        > >
        > > I am not sure whether your problem is as complicated as it sounds.
        > >
        > > Perhaps your problem is just that your thread won't terminate when the
        > > application terminates. If this is the case, then you need the
        > > IsBackground
        > > property...
        > >
        > > Thread myThread = new Thread(...);
        > > myThread.IsBack ground = true;
        > > myThread.Start( );
        > >
        > > Now your thread will terminate when the application terminates.
        > >
        > > However, perhaps your problem is more complicated...
        > >
        > > - You have a class which creates a thread
        > > - Your application creates an instance of the class
        > > - Your application deletes the instance
        > > - Your application continues running
        > > - You thread also continues running. Problem! You expect the thread to
        > > have
        > > been stopped.
        > >
        > > I have done some prototyping, and found that I cannot solve this problem.
        > > I
        > > have created a finalizer for the class, which stops the thread, as
        > > follows:
        > >
        > > class ThreadClass
        > > {
        > > ...
        > >
        > > volatile bool run = false;
        > >
        > > ~ThreadClass()
        > > {
        > > run = false;
        > > }
        > >
        > > void ThreadEntry()
        > > {
        > > run = true;
        > > while (run)
        > > {
        > > Thread.Sleep(30 0);
        > > }
        > > }
        > >
        > > }
        > >
        > > However, I have been unable to force the finalizer to be called. I have
        > > tried to make the instance available for collection, by setting it to
        > > null,
        > > and then forcing garbage collection with GC.Collect(), but this hasn't
        > > worked. The problem here is not terminating the thread, but getting the
        > > finalizer to be called.
        > >
        > > I suggest that you stop the thread from outside, by making a public method
        > > on the thread class...
        > >
        > > class ThreadClass
        > > {
        > > ....
        > > void Stop()
        > > {
        > > run = false;
        > > }
        > > }
        > >
        > > HTH,
        > >
        > > Javaman
        > >
        > >
        > >
        > >
        > >
        > > "Tomaz Koritnik" wrote:
        > >[color=darkred]
        > >> I have a class that runs one of it's method in another thread. I use
        > >> Thread
        > >> object to do this and inside ThreadMethod I have an infinite loop:
        > >>
        > >> While (true)
        > >> {
        > >> // do something
        > >> Thread.Sleep(10 0);
        > >> }
        > >>
        > >> The problem is that I don't know how to terminate the thread when my
        > >> class
        > >> is destroyed. When closing application, destructor of my class is NOT
        > >> called
        > >> so I can't stop thread there. Looks like destructor will be called AFTER
        > >> the
        > >> thread has finished. Interesting and I'm stuck here because I don't know
        > >> where to put the code that stops the thread. Also, I want my class to
        > >> stop
        > >> thread automatically upon destruction and I don't want to call some
        > >> method
        > >> of my class from outside to stop the thread.
        > >>
        > >> I can't believe that I can't solve this problem because doing it in
        > >> Delphi
        > >> it's a child's play :).
        > >>
        > >> regards
        > >> Tomaz
        > >>
        > >>
        > >>[/color]
        > >[/color]
        >
        >
        >[/color]

        Comment

        • Peter Bromberg [C# MVP]

          #5
          Re: Terminating thread

          why not use some boolean class-level variable such as "bRun"

          Before kicking off your thread, you can set this to true.

          When you are ready to stop, just set it to false.

          So ,

          while (bRun)
          {
          // your code

          }

          When you set bRun to false, the loop will terminate.

          Peter

          "Tomaz Koritnik" <nospam@nospam. com> wrote in message
          news:x00Xe.267$ h6.87048@news.s iol.net...[color=blue]
          >I have a class that runs one of it's method in another thread. I use Thread
          >object to do this and inside ThreadMethod I have an infinite loop:
          >
          > While (true)
          > {
          > // do something
          > Thread.Sleep(10 0);
          > }
          >
          > The problem is that I don't know how to terminate the thread when my class
          > is destroyed. When closing application, destructor of my class is NOT
          > called so I can't stop thread there. Looks like destructor will be called
          > AFTER the thread has finished. Interesting and I'm stuck here because I
          > don't know where to put the code that stops the thread. Also, I want my
          > class to stop thread automatically upon destruction and I don't want to
          > call some method of my class from outside to stop the thread.
          >
          > I can't believe that I can't solve this problem because doing it in Delphi
          > it's a child's play :).
          >
          > regards
          > Tomaz
          >[/color]


          Comment

          • Jeff Louie

            #6
            Re: Terminating thread

            As is:

            private void cout_thread()
            {
            try
            {
            // if process does not exit normally, will
            // throw exception on close
            while(!myProces s.HasExited)
            {
            your_cout.Write Line(my_cout.Re adLine());
            }
            your_cout.Write Line(my_cout.Re adToEnd());
            }
            catch (Exception e) {}
            finally
            {
            notifyCoutDone( );
            }
            }

            Regards,
            Jeff

            *** Sent via Developersdex http://www.developersdex.com ***

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Terminating thread

              Peter Bromberg [C# MVP] <pbromberg@yaho o.spamsucks.com > wrote:[color=blue]
              > why not use some boolean class-level variable such as "bRun"
              >
              > Before kicking off your thread, you can set this to true.
              >
              > When you are ready to stop, just set it to false.
              >
              > So ,
              >
              > while (bRun)
              > {
              > // your code
              >
              > }
              >
              > When you set bRun to false, the loop will terminate.[/color]

              Only if bRun is volatile. Otherwise it just *might* terminate. See
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


              --
              Jon Skeet - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              If replying to the group, please do not mail me too

              Comment

              Working...