Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Are child threads of an object destroyed when an object is destroyed.

Are child threads of an object destroyed when an object is destroyed.

Scheduled Pinned Locked Moved C#
debuggingjsonworkspace
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sunset Towers
    wrote on last edited by
    #1

    I have an object that does work in the background and fires events as certain things happen. If this object is destroyed will the thread be automatically terminated. I'm guessing that if the thread is not terminated then I need to implement the IDisposable interface and set Dispose() method. I would rather not do this, but I have in the past had code that kept the debugger running because the child thread was still in operation. At the time however I was using new Thread(new ThreadStart(myThread)).Start() so I assumed that, while the thread was started from the object, it was not contained within the object's running environment and it was not able to be GCed with the rest of the object. I implemented a stopping routine when the program was being terminated. However, I am unable to do the same this time. The thread that is being created is being created as an object private Thread myThread so I'm thinking that the thread would be terminated upon the object being destroyed. I certainly hope this is the case.

    R N B P 4 Replies Last reply
    0
    • S Sunset Towers

      I have an object that does work in the background and fires events as certain things happen. If this object is destroyed will the thread be automatically terminated. I'm guessing that if the thread is not terminated then I need to implement the IDisposable interface and set Dispose() method. I would rather not do this, but I have in the past had code that kept the debugger running because the child thread was still in operation. At the time however I was using new Thread(new ThreadStart(myThread)).Start() so I assumed that, while the thread was started from the object, it was not contained within the object's running environment and it was not able to be GCed with the rest of the object. I implemented a stopping routine when the program was being terminated. However, I am unable to do the same this time. The thread that is being created is being created as an object private Thread myThread so I'm thinking that the thread would be terminated upon the object being destroyed. I certainly hope this is the case.

      R Offline
      R Offline
      Roger Alsing 0
      wrote on last edited by
      #2

      No, threads will live until: * They are finished * They get an exception and exit * They are aborted * Threads with .IsBackground = true will exit when the current process ends (btw. did I miss something?) But if you create a thread inside an object, it will not stop just because your object dies.

      Blog: http://www.rogeralsing.com Projects: http://www.puzzleframework.com

      1 Reply Last reply
      0
      • S Sunset Towers

        I have an object that does work in the background and fires events as certain things happen. If this object is destroyed will the thread be automatically terminated. I'm guessing that if the thread is not terminated then I need to implement the IDisposable interface and set Dispose() method. I would rather not do this, but I have in the past had code that kept the debugger running because the child thread was still in operation. At the time however I was using new Thread(new ThreadStart(myThread)).Start() so I assumed that, while the thread was started from the object, it was not contained within the object's running environment and it was not able to be GCed with the rest of the object. I implemented a stopping routine when the program was being terminated. However, I am unable to do the same this time. The thread that is being created is being created as an object private Thread myThread so I'm thinking that the thread would be terminated upon the object being destroyed. I certainly hope this is the case.

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        A recommended way is to use ThreadPool.QueueUserWorkItem to obtain worker threads for your application rather than creating threads directly. Otherwise you should set Thread.IsBackground = true; This will prevent the app from being held open by any threads that are still running.


        only two letters away from being an asset

        1 Reply Last reply
        0
        • S Sunset Towers

          I have an object that does work in the background and fires events as certain things happen. If this object is destroyed will the thread be automatically terminated. I'm guessing that if the thread is not terminated then I need to implement the IDisposable interface and set Dispose() method. I would rather not do this, but I have in the past had code that kept the debugger running because the child thread was still in operation. At the time however I was using new Thread(new ThreadStart(myThread)).Start() so I assumed that, while the thread was started from the object, it was not contained within the object's running environment and it was not able to be GCed with the rest of the object. I implemented a stopping routine when the program was being terminated. However, I am unable to do the same this time. The thread that is being created is being created as an object private Thread myThread so I'm thinking that the thread would be terminated upon the object being destroyed. I certainly hope this is the case.

          B Offline
          B Offline
          Bekjong
          wrote on last edited by
          #4

          There's a property called IsBackground on your thread, if this is set to true the thread will be terminated when your program exits. If you want to stop the thread at runtime you'll have to do this from code: The garbage collection won't clean the object it's running from until the thread's stopped (the thread still holds a reference to its parent container), this doesn't necessarily have to be from an IDisposable implementation though: If you stop the thread and there's no references to its parent object anymore the object will be taken care of by the gc.

          Standards are great! Everybody should have one!

          1 Reply Last reply
          0
          • S Sunset Towers

            I have an object that does work in the background and fires events as certain things happen. If this object is destroyed will the thread be automatically terminated. I'm guessing that if the thread is not terminated then I need to implement the IDisposable interface and set Dispose() method. I would rather not do this, but I have in the past had code that kept the debugger running because the child thread was still in operation. At the time however I was using new Thread(new ThreadStart(myThread)).Start() so I assumed that, while the thread was started from the object, it was not contained within the object's running environment and it was not able to be GCed with the rest of the object. I implemented a stopping routine when the program was being terminated. However, I am unable to do the same this time. The thread that is being created is being created as an object private Thread myThread so I'm thinking that the thread would be terminated upon the object being destroyed. I certainly hope this is the case.

            P Offline
            P Offline
            pbraun
            wrote on last edited by
            #5

            The best solution for this is to use a flag in each thread that when set to true allows the thread to run. When this same flag is set to false, the thread exits. Then in the dispose method, set the flag to false and the threads will exit. This is called a co-operative exit which allows the thread to complete its current task before exiting. Using thread pool threads to complete your work may seem like a good idea, but one must remember that thread pool threads are not executed if the system is busy. In otherwords, if the work being done in a thread is critical, it should not be put in a thread pool thread. Using thread.abort() to terminate the thread means that if the thread will be interrupted during its execution and told to terminate. If the task being performed by the thread is critical and must be completed, using thread.abort() will at some point interrupt that task and cause problems.

            Phil

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups