none
Thread Life Cycle RRS feed

  • Question

  •  
       A colleague of mine has suggested that one can create a thread from within a Win form, start the execution of the thread, and the thread will continue to process even if the form is closed. I'm new to threading, but my first thoughts are that all objects created within the form, including the instance of the thread, will be disposed when the form closes. However, I suppose it's possible that the thread runs in an enitirely memory space, and therefore is not affected by the form being disposed.

      Can somebody please clear things up?

    Thanks,
    Attila
    Friday, July 18, 2008 8:07 PM

Answers

  • The thread has a property called IsBackground.  (http://msdn.microsoft.com/en-us/library/system.threading.thread.isbackground.aspx)

    When IsBackground = true, the exe can exit even if the thread is still running.
    When IsBackground = false (the default), the exe will not exit until the thread has finished.

    However, in neither case does any references to the Thread object held by your code control its execution lifetime.  That is entirely controlled by the above two settings.

    In other words, to sum it up, the runtime holds its own references to the Thread which control the Thread's lifetime even after all of your references to the Thread have gone away.


    • Edited by Jason Kresowaty Saturday, July 19, 2008 8:54 PM Fixed. Thanks for the debug, nobugz!
    • Marked as answer by Zhi-Xin Ye Thursday, July 24, 2008 2:46 AM
    Friday, July 18, 2008 11:56 PM

All replies

  • Attila226 said:

     
       A colleague of mine has suggested that one can create a thread from within a Win form, start the execution of the thread, and the thread will continue to process even if the form is closed...


    Your colleague is correct. A form and a thread started from that form do not have any inherent knowledge of each other. Closing the form does not end the thread.

    However, if the form is the main form of the app the CLR will try to shut down. If the thread you created is a background thread the CLR won’t pay it much attention and will shut down anyway. If the thread is *not* a background thread the CLR will wait for that thread to terminate on its own before shutting down. (Your form will be gone, but you can still see the process running in Task Manager.) See http://msdn.microsoft.com/en-us/library/system.threading.thread.isbackground.aspx.

    Memory and address space have nothing to do with this discussion.



    Curt - http://www.codeneverwritten.com/
    Friday, July 18, 2008 11:53 PM
  • The thread has a property called IsBackground.  (http://msdn.microsoft.com/en-us/library/system.threading.thread.isbackground.aspx)

    When IsBackground = true, the exe can exit even if the thread is still running.
    When IsBackground = false (the default), the exe will not exit until the thread has finished.

    However, in neither case does any references to the Thread object held by your code control its execution lifetime.  That is entirely controlled by the above two settings.

    In other words, to sum it up, the runtime holds its own references to the Thread which control the Thread's lifetime even after all of your references to the Thread have gone away.


    • Edited by Jason Kresowaty Saturday, July 19, 2008 8:54 PM Fixed. Thanks for the debug, nobugz!
    • Marked as answer by Zhi-Xin Ye Thursday, July 24, 2008 2:46 AM
    Friday, July 18, 2008 11:56 PM
  • Edit your post Jason, you've got it reversed.
    Hans Passant.
    Saturday, July 19, 2008 7:01 PM
    Moderator