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. CWinThread for background processing

CWinThread for background processing

Scheduled Pinned Locked Moved C / C++ / MFC
designquestionannouncement
7 Posts 4 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.
  • Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    I have a dilemma over whether I should create a User Interface thread or a Background thread to process my background operations. The docs for the function SHGetFileInfo say that it should not be called from the application's main thread because it can lock up the UI. So I will use a secondary thread. And I will post messages to the thread to prompt it to process stuff. But should I use the UI thread version of AfxBeginThread because it includes a message pump, or should I use the background thread version and implement my own message pump? I ask because I do not want the application to terminate when the secondary thread terminates. My understanding of the docs is that that could happen if I use a UI thread. Thank you

    The difficult we do right away... ...the impossible takes slightly longer.

    Mircea NeacsuM L 2 Replies Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      I have a dilemma over whether I should create a User Interface thread or a Background thread to process my background operations. The docs for the function SHGetFileInfo say that it should not be called from the application's main thread because it can lock up the UI. So I will use a secondary thread. And I will post messages to the thread to prompt it to process stuff. But should I use the UI thread version of AfxBeginThread because it includes a message pump, or should I use the background thread version and implement my own message pump? I ask because I do not want the application to terminate when the secondary thread terminates. My understanding of the docs is that that could happen if I use a UI thread. Thank you

      The difficult we do right away... ...the impossible takes slightly longer.

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      I don't have personal experience with this function but Microsoft doc seems pretty specific:

      Quote:

      You should call this function from a background thread. Failure to do so could cause the UI to stop responding.

      So background thread it is! :)

      Mircea

      Richard Andrew x64R 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        I have a dilemma over whether I should create a User Interface thread or a Background thread to process my background operations. The docs for the function SHGetFileInfo say that it should not be called from the application's main thread because it can lock up the UI. So I will use a secondary thread. And I will post messages to the thread to prompt it to process stuff. But should I use the UI thread version of AfxBeginThread because it includes a message pump, or should I use the background thread version and implement my own message pump? I ask because I do not want the application to terminate when the secondary thread terminates. My understanding of the docs is that that could happen if I use a UI thread. Thank you

        The difficult we do right away... ...the impossible takes slightly longer.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Hi,

        Richard Andrew x64 wrote:

        I ask because I do not want the application to terminate when the secondary thread terminates. My understanding of the docs is that that could happen if I use a UI thread.

        Could you show me what you are referring to? The only way that I am aware of... where a CWinThread can terminate the main thread is if you reach across threads to execute code. If a WM_QUIT arrives during that short period I believe it gets posted to the main threads message queue. Don't do this:

        ThreadA->ThreadB_DoSomething();
        ThreadB->ThreadA_Dosomething();

        Instead post a message WM_THREAD_A_DOSOMETHING As long as ThreadA never touches ThreadB you shouldn't have any problems. Also don't create any windows or enter any modal modal loops from your CWinThread worker thread... do all window management from your main thread. Follow these rules: 1.) Do all window creation in your main thread. 2.) Don't reach across threads to execute code. 3.) Use PostMessage to communicate between threads. You *can* break these rules but make sure that you fully understand the consequences and how to get around them. Raymond Chen explains how you can get around messages are eaten by modal loops[^] here. Best Wishes, -David Delaune

        Richard Andrew x64R 1 Reply Last reply
        0
        • L Lost User

          Hi,

          Richard Andrew x64 wrote:

          I ask because I do not want the application to terminate when the secondary thread terminates. My understanding of the docs is that that could happen if I use a UI thread.

          Could you show me what you are referring to? The only way that I am aware of... where a CWinThread can terminate the main thread is if you reach across threads to execute code. If a WM_QUIT arrives during that short period I believe it gets posted to the main threads message queue. Don't do this:

          ThreadA->ThreadB_DoSomething();
          ThreadB->ThreadA_Dosomething();

          Instead post a message WM_THREAD_A_DOSOMETHING As long as ThreadA never touches ThreadB you shouldn't have any problems. Also don't create any windows or enter any modal modal loops from your CWinThread worker thread... do all window management from your main thread. Follow these rules: 1.) Do all window creation in your main thread. 2.) Don't reach across threads to execute code. 3.) Use PostMessage to communicate between threads. You *can* break these rules but make sure that you fully understand the consequences and how to get around them. Raymond Chen explains how you can get around messages are eaten by modal loops[^] here. Best Wishes, -David Delaune

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          Thank you for the detailed response. So you would recommend the UI version of CWinThread because it has a message pump already?

          The difficult we do right away... ...the impossible takes slightly longer.

          L 1 Reply Last reply
          0
          • Mircea NeacsuM Mircea Neacsu

            I don't have personal experience with this function but Microsoft doc seems pretty specific:

            Quote:

            You should call this function from a background thread. Failure to do so could cause the UI to stop responding.

            So background thread it is! :)

            Mircea

            Richard Andrew x64R Offline
            Richard Andrew x64R Offline
            Richard Andrew x64
            wrote on last edited by
            #5

            Yes, I was taken aback when I first saw that.

            The difficult we do right away... ...the impossible takes slightly longer.

            1 Reply Last reply
            0
            • Richard Andrew x64R Richard Andrew x64

              Thank you for the detailed response. So you would recommend the UI version of CWinThread because it has a message pump already?

              The difficult we do right away... ...the impossible takes slightly longer.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Well,

              Richard Andrew x64 wrote:

              So you would recommend the UI version of CWinThread because it has a message pump already?

              Even if you used a non-gui thread.... the Windows kernel auto-promotes a thread via KiConvertToGuiThread and increases the thread stack-size and gives it a message queue immediately when it makes a syscall[^] above 0x1000. In other words... as soon as you make your call to SHGetFileInfo[^] your thread will become a UI thread. You could prevent the auto-promotion by setting PROCESS_CREATION_MITIGATION_POLICY_WIN32K_SYSTEM_CALL_DISABLE_ALWAYS_ON but that would break your SHGetFileInfo and a myriad of other win32k system calls. You can use CWinThread and just follow the rules I laid out in my previous response. Best Wishes, -David Delaune

              CPalliniC 1 Reply Last reply
              0
              • L Lost User

                Well,

                Richard Andrew x64 wrote:

                So you would recommend the UI version of CWinThread because it has a message pump already?

                Even if you used a non-gui thread.... the Windows kernel auto-promotes a thread via KiConvertToGuiThread and increases the thread stack-size and gives it a message queue immediately when it makes a syscall[^] above 0x1000. In other words... as soon as you make your call to SHGetFileInfo[^] your thread will become a UI thread. You could prevent the auto-promotion by setting PROCESS_CREATION_MITIGATION_POLICY_WIN32K_SYSTEM_CALL_DISABLE_ALWAYS_ON but that would break your SHGetFileInfo and a myriad of other win32k system calls. You can use CWinThread and just follow the rules I laid out in my previous response. Best Wishes, -David Delaune

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                Wow! :thumbsup:

                In testa che avete, signor di Ceprano?

                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