Solved

When MFC application terminates

Posted on 2003-11-26
4
407 Views
Last Modified: 2013-11-20
I call PostQuitMessage to main UI thread of MFC, I have other threads in the app.
Will MFC app terminate or it will wait until all threads terminate ?
0
Comment
Question by:amakalski
  • 3
4 Comments
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
All threads will be terminated. However, good programming style requires to stop all worker threads before exit. Every worker thread should have stop event associated with it. When main thread needs to stop the worker thread, it executes the following code:

SetEvent(hStopEvent);   // ask thread to exit
WaitForSingleObject(hThread, INFINITE);  // wait when thread really exits

Thread function should check stop event periodically:

while (...)   // some loop
{
    // do something

    if ( WaitForSingleObject(hStopEvent, 0) == WAIT_OBJECT_0 )  // stopped ?
        break;
}

// clean-up and return

0
 

Author Comment

by:amakalski
Comment Utility
AlexFM,
Do you say that after posting QUIT msg to main UI thread, app will be terminated and other threads will be killed? Is that right?
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
Comment Utility
Yes.
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
I apologize for providing wrong information. PostQuitMessage doesn't terminate all threads in process. It terminates only UI thread to which it posted. ExitProcess and TerminateProcess functions terminate process and all it's threads.
In any case, all worker threads should be stopped before exit using the way shown in my first post.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now