Closing an application.
-
I have an SDI application that is supposed to close it self when finished. To do this it sends a m_pMainFrame->PostMessage(WM_SYSCOMMAND, SC_CLOSE); The application appears to stop running and it gives no errors or warnings in release or debug mode. However, task manager almost always shows the application is still in memory. Is there a more forceful way to close an app. P.S. I have also tried combinations of SW_CLOSE and SendMessage(). Thanks
-
I have an SDI application that is supposed to close it self when finished. To do this it sends a m_pMainFrame->PostMessage(WM_SYSCOMMAND, SC_CLOSE); The application appears to stop running and it gives no errors or warnings in release or debug mode. However, task manager almost always shows the application is still in memory. Is there a more forceful way to close an app. P.S. I have also tried combinations of SW_CLOSE and SendMessage(). Thanks
-
I have an SDI application that is supposed to close it self when finished. To do this it sends a m_pMainFrame->PostMessage(WM_SYSCOMMAND, SC_CLOSE); The application appears to stop running and it gives no errors or warnings in release or debug mode. However, task manager almost always shows the application is still in memory. Is there a more forceful way to close an app. P.S. I have also tried combinations of SW_CLOSE and SendMessage(). Thanks
-
I have an SDI application that is supposed to close it self when finished. To do this it sends a m_pMainFrame->PostMessage(WM_SYSCOMMAND, SC_CLOSE); The application appears to stop running and it gives no errors or warnings in release or debug mode. However, task manager almost always shows the application is still in memory. Is there a more forceful way to close an app. P.S. I have also tried combinations of SW_CLOSE and SendMessage(). Thanks
-
I have an SDI application that is supposed to close it self when finished. To do this it sends a m_pMainFrame->PostMessage(WM_SYSCOMMAND, SC_CLOSE); The application appears to stop running and it gives no errors or warnings in release or debug mode. However, task manager almost always shows the application is still in memory. Is there a more forceful way to close an app. P.S. I have also tried combinations of SW_CLOSE and SendMessage(). Thanks
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 cas
-
-
Generally the method I use is to send the ID_APP_EXIT command to the main window. This will then use the same exit procedure as if the user clicked "Exit" from the File menu. For example: AfxGetMainWnd()->SendMessage( WM_COMMAND, ID_APP_EXIT );
-
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 cas