Solved

When MFC application terminates

Posted on 2003-11-26
4
413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Include multiple hostnames in this command? 3 56
sumNumber challenge 16 145
if loop in java 3 174
Way to decrease size of apk file 9 132
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Suggested Courses

732 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