Click here to Skip to main content
15,892,965 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Icon On Tabctrl Pin
LiYS15-Jun-05 15:24
LiYS15-Jun-05 15:24 
GeneralClosing an application. Pin
mcgahanfl15-Jun-05 3:31
mcgahanfl15-Jun-05 3:31 
GeneralRe: Closing an application. Pin
Bob Stanneveld15-Jun-05 3:48
Bob Stanneveld15-Jun-05 3:48 
GeneralRe: Closing an application. Pin
mcgahanfl16-Jun-05 3:24
mcgahanfl16-Jun-05 3:24 
GeneralRe: Closing an application. Pin
David Crow15-Jun-05 4:50
David Crow15-Jun-05 4:50 
GeneralRe: Closing an application. Pin
GKarRacer15-Jun-05 13:44
GKarRacer15-Jun-05 13:44 
GeneralRe: Closing an application. Pin
mcgahanfl16-Jun-05 3:25
mcgahanfl16-Jun-05 3:25 
GeneralRe: Closing an application. Pin
Toby Opferman15-Jun-05 14:24
Toby Opferman15-Jun-05 14:24 
I think you first need to understand how the application executes and how an application terminates. It seems as though you are using MFC which hides how things work and most likely why you don't see why this doesn't terminate the application.

A process terminates when "ExitProcess" is called. Simple, right? Just as a thread terminates when "ExitThread" is called. Also pretty simple, right?

So, do you need to call ExitProcess and ExitThread in your application? No, you don't! The main thread of an application will call "ExitProcess" once you return from the "WinMain" or "main" API. There is wrapper code around this thread which will call this for you (unless you've turned this code generation off which is not likely if you are using MFC).

So, once the main thread exits the process will exit. What about threads? An individual thread will terminate also once it returns. "CreateThread" and "BeginThread" do not start a thread as your function but actually start a wrapper function which calls your threadproc. The exit code again will call "ExitThread" so you do not need to call this API yourself.

So, why isn't your application closing? Well the main thread never exited. You should nicely terminate all other threads, however the reason is that your main thread never exited. So why wouldn't it exit?

Well, you called "WM_CLOSE" and that's nice. Your window closes, but the thread didn't terminate if this is your main thread. So, what happens? You need to get the "Message Loop" to quit. The message loop looks like this generally:

while(GetMessage(...))
{   
    TranslateMessage(...);
    DispatchMessage(...);
}


This message loop is dupliated a lot. As an example, "MessageBox" API implements it's own however these are speical. They will exit when the window goes away. The way the one above works is that it will only exit if GetMessage returns 0, which it only does when WM_QUIT is processed. How is this processed? By using PostQuitMessage(); which poses the quit message to that thread.

So, it's generally considered appropriate to call "DestroyWindow()" then in your WM_DESTROY (or in WM_CLOSE call DestroyWindow() as well, but you should handle clean up appropriately) to then call "PostQuitMessage" to exit the thread. This way you clean up properly, send the quit message so the loop exits and the thread should then exit. (Well, the message loop exits at least, doesn't mean that the thread would exit but generally there's no code after this so usually it's the case).

If this is the main thread then your application should exit. If it is not your main thread then you need to also do whatever it takes to get your main thread to exit.

If this is your main thread and it doesn't exit there could still be reasons. As an example in order for it to exit it will possibly need to grab the loader lock or even the heap lock, which could have been held by a terminating thread or have some deadlock contention with something else in your application.

The best thing to do is get the debugger fired up and find out what's going on in that case.




8bc7c0ec02c0e404c0cc0680f7018827ebee
GeneralRe: Closing an application. Pin
mcgahanfl16-Jun-05 3:26
mcgahanfl16-Jun-05 3:26 
GeneralBuilding question Pin
Lampros Giampouras15-Jun-05 2:49
Lampros Giampouras15-Jun-05 2:49 
GeneralRe: Building question Pin
Cedric Moonen15-Jun-05 3:24
Cedric Moonen15-Jun-05 3:24 
GeneralRe: Building question Pin
normanS15-Jun-05 3:53
normanS15-Jun-05 3:53 
GeneralRe: Building question Pin
Lampros Giampouras15-Jun-05 14:09
Lampros Giampouras15-Jun-05 14:09 
GeneralRe: Building question Pin
squidev15-Jun-05 15:46
squidev15-Jun-05 15:46 
GeneralRe: Building question Pin
Lampros Giampouras16-Jun-05 9:11
Lampros Giampouras16-Jun-05 9:11 
Generalweird errors Pin
Anonymous15-Jun-05 2:29
Anonymous15-Jun-05 2:29 
GeneralRe: weird errors Pin
Blake Miller15-Jun-05 4:44
Blake Miller15-Jun-05 4:44 
GeneralRe: weird errors Pin
David Crow15-Jun-05 5:40
David Crow15-Jun-05 5:40 
GeneralCListCtrl Class Help! Pin
mahanagarjuna15-Jun-05 2:12
mahanagarjuna15-Jun-05 2:12 
GeneralMessage for ListView Pin
Duy Nghia15-Jun-05 1:27
Duy Nghia15-Jun-05 1:27 
GeneralRe: Message for ListView Pin
David Crow15-Jun-05 4:53
David Crow15-Jun-05 4:53 
GeneralRe: Message for ListView Pin
Duy Nghia15-Jun-05 15:35
Duy Nghia15-Jun-05 15:35 
GeneralRe: Message for ListView Pin
David Crow16-Jun-05 2:47
David Crow16-Jun-05 2:47 
GeneralCreating Multiple Document Interface using a CRichEditCtrl Pin
dittygrail15-Jun-05 1:17
dittygrail15-Jun-05 1:17 
GeneralOwner Draw List Box & GetText() Pin
Jon Hulatt15-Jun-05 1:14
Jon Hulatt15-Jun-05 1:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.