Solved

When MFC application terminates

Posted on 2003-11-26
4
412 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
ID: 9826974
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
ID: 9827037
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
ID: 9827048
Yes.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9830038
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Whole sheet autoscrub still needed 19 52
How to know only "File created" with EventLog 3 35
Line meaning 9 88
unix example issues 18 99
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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