CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2008
    Posts
    98

    What will happen in long-working thread when main thread exit?

    In the main thread when click a button, then begin the long-working thread,
    before the long-working thread finish it's work, then the user click the exit button
    to exit the application! And in the long-working thread I CreateFile to use all long
    with the thread life time.
    In such situation, how will the long-working thread know when user exit the application?
    how to properly clean up the thread.
    Thanks in advance!

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: What will happen in long-working thread when main thread exit?

    The usual way is using Events and WaitForSingleObject/WaitForMultipleObjects APIs.
    See also Using Worker Threads
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: What will happen in long-working thread when main thread exit?

    The point is to synchronize the threads. When you close the application you should notify the worker thread to stop. In the GUI thread you should wait for the worker thread(s) to finish and only after that continue closing.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: What will happen in long-working thread when main thread exit?

    To finish up...

    If your main thread exits, all other threads that you created will also get aborted right there and then, no cleanup is called. Depending on what your worker thread does, this could lead to all kinds of nasties like unsaved files, corrupted data, resource leaks etc.

    In short: the main thread should always wait for worker threads to finish normally. Anything other than that is just sloppy or lazyness on your part at best and can lead to angry customers, lost revenue and long hours wasted on support, debugging and "damage cleanup".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured