Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Closing an application.

Closing an application.

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingperformanceannouncement
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mcgahanfl
    wrote on last edited by
    #1

    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

    B D G T 4 Replies Last reply
    0
    • M mcgahanfl

      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

      B Offline
      B Offline
      Bob Stanneveld
      wrote on last edited by
      #2

      Hello, MSDN says that SC_CLOSE will only close the window. What happens if you try PostQuitMessage()? Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

      M 1 Reply Last reply
      0
      • M mcgahanfl

        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

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        See here.


        "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

        1 Reply Last reply
        0
        • M mcgahanfl

          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

          G Offline
          G Offline
          GKarRacer
          wrote on last edited by
          #4

          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 );

          M 1 Reply Last reply
          0
          • M mcgahanfl

            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

            T Offline
            T Offline
            Toby Opferman
            wrote on last edited by
            #5

            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

            M 1 Reply Last reply
            0
            • B Bob Stanneveld

              Hello, MSDN says that SC_CLOSE will only close the window. What happens if you try PostQuitMessage()? Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

              M Offline
              M Offline
              mcgahanfl
              wrote on last edited by
              #6

              Thank you. That worked nicely.

              1 Reply Last reply
              0
              • G GKarRacer

                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 );

                M Offline
                M Offline
                mcgahanfl
                wrote on last edited by
                #7

                Thank you. This is good information.

                1 Reply Last reply
                0
                • T Toby Opferman

                  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

                  M Offline
                  M Offline
                  mcgahanfl
                  wrote on last edited by
                  #8

                  Thank you. I very much appreciate the background information.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups