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. [MFC] Keep thread alive until application exits

[MFC] Keep thread alive until application exits

Scheduled Pinned Locked Moved C / C++ / MFC
c++mobilehelpquestion
19 Posts 8 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.
  • E Erik

    Hi, I use the CWinThread class to have my CDialog GUI stay responsive while doing some lengthy calculation. However, my thread needs to stay alive, since it can receive some notification anytime while my application runs. The problem I have is that after InitInstance is done, my thread automatically enters the ExitInstance method, which I do not want. However, I want my thread to stay alive until I close my CDialog, and then enter ExitInstance. Any idea how this can be performed? Thanks alot.

    R Offline
    R Offline
    Rajesh R Subramanian
    wrote on last edited by
    #9

    If you need to perform a lengthy calculation, spawn a worker thread and let it do that. The UI thread should not be burdened with anything else but responding to the user.

    "Real men drive manual transmission" - Rajesh.

    1 Reply Last reply
    0
    • R Rajesh R Subramanian

      Absolutely! I've actually seen people overriding the run method and squeeze in an infinite loop there. When I tell them "that's what it already does, may be what you need is a worker thread?", they'll be like "oh yeah, that makes sense". A lot of people using spawning UI threads don't actually know that they're spawning a, well, UI thread.

      "Real men drive manual transmission" - Rajesh.

      A Offline
      A Offline
      Albert Holguin
      wrote on last edited by
      #10

      :laugh: I use CWinThreads quite a bit... I actually like them, they're easy to use and communicate with...

      1 Reply Last reply
      0
      • R Roger Allen

        From your description it sounds like you have your threads backwards. Your doing the work in a UI thread and trying to spawn another UI thread to keep your UI active. In fact the UI thread should start a worker thread to do the lengthy operation. Then the UI thread can continue to service the user actions.

        If you vote me down, my score will only get lower

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #11

        Excellent answer, Roger.

        "Real men drive manual transmission" - Rajesh.

        1 Reply Last reply
        0
        • R Roger Allen

          From your description it sounds like you have your threads backwards. Your doing the work in a UI thread and trying to spawn another UI thread to keep your UI active. In fact the UI thread should start a worker thread to do the lengthy operation. Then the UI thread can continue to service the user actions.

          If you vote me down, my score will only get lower

          A Offline
          A Offline
          Albert Holguin
          wrote on last edited by
          #12

          The UI name is simply a naming convention that MS picked up a long time ago, it doesn't necessarily mean that a CWinThread needs to work with a user interface. Its useful when you need a working thread that's persistent, meaning it doesn't do a job then end.

          R 1 Reply Last reply
          0
          • R Rajesh R Subramanian

            Absolutely! I've actually seen people overriding the run method and squeeze in an infinite loop there. When I tell them "that's what it already does, may be what you need is a worker thread?", they'll be like "oh yeah, that makes sense". A lot of people using spawning UI threads don't actually know that they're spawning a, well, UI thread.

            "Real men drive manual transmission" - Rajesh.

            A Offline
            A Offline
            Albert Holguin
            wrote on last edited by
            #13

            By the way, see my note[^] below regarding the UI naming convention.

            1 Reply Last reply
            0
            • A Albert Holguin

              The UI name is simply a naming convention that MS picked up a long time ago, it doesn't necessarily mean that a CWinThread needs to work with a user interface. Its useful when you need a working thread that's persistent, meaning it doesn't do a job then end.

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #14

              If you're implying that "UI thread" is some kind of an inappropriate name, I can't agree with you more. I've used these "UI" threads in several instances where there's no UI. For example, if I needed some kind of a queued event processing mechanism within my program that will process requests in the order they were received, then UI thread would be a quick and reliable way to do it. :) BTW, Roger is correct in saying that the OP should rather delegate lengthy tasks to a background worker, and not bother the main thread that processes the dialog UI events.

              "Real men drive manual transmission" - Rajesh.

              A 1 Reply Last reply
              0
              • R Rajesh R Subramanian

                If you're implying that "UI thread" is some kind of an inappropriate name, I can't agree with you more. I've used these "UI" threads in several instances where there's no UI. For example, if I needed some kind of a queued event processing mechanism within my program that will process requests in the order they were received, then UI thread would be a quick and reliable way to do it. :) BTW, Roger is correct in saying that the OP should rather delegate lengthy tasks to a background worker, and not bother the main thread that processes the dialog UI events.

                "Real men drive manual transmission" - Rajesh.

                A Offline
                A Offline
                Albert Holguin
                wrote on last edited by
                #15

                Exactly! :-D Don't know exactly where that name started, but it stuck apparently. Should be more of a "Persistent thread" or something similar.

                1 Reply Last reply
                0
                • C Chuck OToole

                  Before creating the thread, create an "Event" object, closed, not auto-reset. When the thread is ready to exit, use WaitForSingleObject() to wait for the event to be triggered. When the dialog closes, have use SetEvent() to tell the thread that it is ok for it to exit now.

                  A Offline
                  A Offline
                  Albert Holguin
                  wrote on last edited by
                  #16

                  CWinThreads don't require this... you can WaitForSingleObject() on the thread handle itself after you've asked the thread to go down with a WM_QUIT message.

                  C 1 Reply Last reply
                  0
                  • A Albert Holguin

                    CWinThreads don't require this... you can WaitForSingleObject() on the thread handle itself after you've asked the thread to go down with a WM_QUIT message.

                    C Offline
                    C Offline
                    Chuck OToole
                    wrote on last edited by
                    #17

                    In my opinion, interthread activities should be explicitly stated rather than rely on side effects of other things. A "permission to exit" signal is one way of formalizing those activities. "Permission to start" is another. The fact that a thread was created doesn't necessarily mean that it can immediately being doing processing. My threads always go through several stages: 1) Initialization at the start address of the thread 2) Signals an "initialization done" event to allow the main thread to create more, knowing that any data structures, other events / objects are created and that the created thread is ready to run 3) Waits for a "permission to start" event. This allows the main thread to delay start of the thread until other threads are created and ensures that subsequent threads have created their objects that may be necessary for interthread communication to work. 4) enters the "run state" where it is now doing its job 5) "permission to exit" is optional and only when you want the "dance of the threads" to end in an orderly fashion. You want this orderly approach to startup and shutdown to ensure that if you create many threads, some of which need to communicate with each other, those threads do not run into timing problems and access objects that may not be created / initialized yet by other threads. Also, I have no idea why you downvoted my answer, it was not wrong, just not to your liking.

                    A 1 Reply Last reply
                    0
                    • C Chuck OToole

                      In my opinion, interthread activities should be explicitly stated rather than rely on side effects of other things. A "permission to exit" signal is one way of formalizing those activities. "Permission to start" is another. The fact that a thread was created doesn't necessarily mean that it can immediately being doing processing. My threads always go through several stages: 1) Initialization at the start address of the thread 2) Signals an "initialization done" event to allow the main thread to create more, knowing that any data structures, other events / objects are created and that the created thread is ready to run 3) Waits for a "permission to start" event. This allows the main thread to delay start of the thread until other threads are created and ensures that subsequent threads have created their objects that may be necessary for interthread communication to work. 4) enters the "run state" where it is now doing its job 5) "permission to exit" is optional and only when you want the "dance of the threads" to end in an orderly fashion. You want this orderly approach to startup and shutdown to ensure that if you create many threads, some of which need to communicate with each other, those threads do not run into timing problems and access objects that may not be created / initialized yet by other threads. Also, I have no idea why you downvoted my answer, it was not wrong, just not to your liking.

                      A Offline
                      A Offline
                      Albert Holguin
                      wrote on last edited by
                      #18

                      Chuck O'Toole wrote:

                      Also, I have no idea why you downvoted my answer, it was not wrong, just not to your liking.

                      CWinThreads aren't used in this fashion... besides, trace back on your other solutions... I'm sure I've upvoted you numerous times (I know I have). Keep up the responses, I'm sure you'll get plenty more :thumbsup: from me.

                      1 Reply Last reply
                      0
                      • E Erik

                        Hi, I use the CWinThread class to have my CDialog GUI stay responsive while doing some lengthy calculation. However, my thread needs to stay alive, since it can receive some notification anytime while my application runs. The problem I have is that after InitInstance is done, my thread automatically enters the ExitInstance method, which I do not want. However, I want my thread to stay alive until I close my CDialog, and then enter ExitInstance. Any idea how this can be performed? Thanks alot.

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

                        I briefly talk about this in this article.

                        "One man's wage rise is another man's price increase." - Harold Wilson

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

                        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