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. GUI Freezes randomly /MFC /C++

GUI Freezes randomly /MFC /C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++designhelp
7 Posts 5 Posters 33 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.
  • Z Offline
    Z Offline
    Zishan Ud787
    wrote on last edited by
    #1

    I am facing this odd issue in which GUI freezes randomly on button clicked So basically This is the sample code: void dialog::OnButton1Clicked() { m_threadCmd = Button1; //Button1 is an enum from commandEnumList AfxBeginThread(ui_ThreadExecuteCmd, this); } UINT dialog::ui_ThreadExecuteCmd() { OWaitCursor waitCursor; switch (this->m_threadCmd) { case BUTTON1: if (!Func1()) { ShowError(); } break; } this->m_threadCmd = 0; return 0; } bool Func1() { //Notify GUI via PostMessage to disable the controls //Do some processing //Notify GUI via PostMessage to enable the controls //return errorcode } The weird thing is that sometime it works fine and sometimes the GUI freezes. I am seeing the UI buttons getting disabled then performing the operation -then ui buttons getting enabled back again. The Gui freeze happens randomly after that and when it freezes the buttons become unclickable and i am not receiving any message in PreTranslateMsg() after the freeze for mouse click or anything else

    Richard Andrew x64R D D 3 Replies Last reply
    0
    • Z Zishan Ud787

      I am facing this odd issue in which GUI freezes randomly on button clicked So basically This is the sample code: void dialog::OnButton1Clicked() { m_threadCmd = Button1; //Button1 is an enum from commandEnumList AfxBeginThread(ui_ThreadExecuteCmd, this); } UINT dialog::ui_ThreadExecuteCmd() { OWaitCursor waitCursor; switch (this->m_threadCmd) { case BUTTON1: if (!Func1()) { ShowError(); } break; } this->m_threadCmd = 0; return 0; } bool Func1() { //Notify GUI via PostMessage to disable the controls //Do some processing //Notify GUI via PostMessage to enable the controls //return errorcode } The weird thing is that sometime it works fine and sometimes the GUI freezes. I am seeing the UI buttons getting disabled then performing the operation -then ui buttons getting enabled back again. The Gui freeze happens randomly after that and when it freezes the buttons become unclickable and i am not receiving any message in PreTranslateMsg() after the freeze for mouse click or anything else

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

      Is it possible that you're performing a lengthy operation on the user interface thread, and that's why it's freezing? From the "code" that you posted, it looks like that's exactly what you're doing. If you do anything non-trivial when the button is clicked, then you should assign the task to a worker thread so that the UI thread is free to process UI inputs.

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

      J Z 2 Replies Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        Is it possible that you're performing a lengthy operation on the user interface thread, and that's why it's freezing? From the "code" that you posted, it looks like that's exactly what you're doing. If you do anything non-trivial when the button is clicked, then you should assign the task to a worker thread so that the UI thread is free to process UI inputs.

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

        J Offline
        J Offline
        jeron1
        wrote on last edited by
        #3

        Oddly, they post different 'code' in QA. GUI freezes randomly /MFC/C++[^]

        "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

        Z 1 Reply Last reply
        0
        • Z Zishan Ud787

          I am facing this odd issue in which GUI freezes randomly on button clicked So basically This is the sample code: void dialog::OnButton1Clicked() { m_threadCmd = Button1; //Button1 is an enum from commandEnumList AfxBeginThread(ui_ThreadExecuteCmd, this); } UINT dialog::ui_ThreadExecuteCmd() { OWaitCursor waitCursor; switch (this->m_threadCmd) { case BUTTON1: if (!Func1()) { ShowError(); } break; } this->m_threadCmd = 0; return 0; } bool Func1() { //Notify GUI via PostMessage to disable the controls //Do some processing //Notify GUI via PostMessage to enable the controls //return errorcode } The weird thing is that sometime it works fine and sometimes the GUI freezes. I am seeing the UI buttons getting disabled then performing the operation -then ui buttons getting enabled back again. The Gui freeze happens randomly after that and when it freezes the buttons become unclickable and i am not receiving any message in PreTranslateMsg() after the freeze for mouse click or anything else

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          You're lucky it works at all. The sample code you posted posts messages to disable the controls, but those message should never get processes because the UI thread is still busy with "Do some logical processing," so it never processes those messages to disable the controls. The processing of the messages will stay queued up until the UI thread is done with your "Do some logical processing" and returns after posting the messages to re-enable the controls. Once the UI thread is back in the "idle state", meaning your back in the message pump code picking up messages and dispatching them, only then will the messages you posted get processed. So that's where your app is "freezing". Like Richard said, if you've got long-running processing going on, move that processing to a task or background thread. You'll also have to rewrite your code to wait for the task to complete before posting the messages to re-enable the controls. The goal is to keep the UI thread available and running in the message pump for as much as possible.

          Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles. Dave Kreskowiak

          1 Reply Last reply
          0
          • J jeron1

            Oddly, they post different 'code' in QA. GUI freezes randomly /MFC/C++[^]

            "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

            Z Offline
            Z Offline
            Zishan Ud787
            wrote on last edited by
            #5

            Hi @jeron1 , Thanks for pointing it out. Updated the question there as well .

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

              Is it possible that you're performing a lengthy operation on the user interface thread, and that's why it's freezing? From the "code" that you posted, it looks like that's exactly what you're doing. If you do anything non-trivial when the button is clicked, then you should assign the task to a worker thread so that the UI thread is free to process UI inputs.

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

              Z Offline
              Z Offline
              Zishan Ud787
              wrote on last edited by
              #6

              Thanks for the reply. It's not a lenghty operation (Hardly takes 1 sec) Also,I am doing that only assigning the task to a worker thread. I have updated the code flow in the question , It will give you the exact idea about how am i doing it.

              1 Reply Last reply
              0
              • Z Zishan Ud787

                I am facing this odd issue in which GUI freezes randomly on button clicked So basically This is the sample code: void dialog::OnButton1Clicked() { m_threadCmd = Button1; //Button1 is an enum from commandEnumList AfxBeginThread(ui_ThreadExecuteCmd, this); } UINT dialog::ui_ThreadExecuteCmd() { OWaitCursor waitCursor; switch (this->m_threadCmd) { case BUTTON1: if (!Func1()) { ShowError(); } break; } this->m_threadCmd = 0; return 0; } bool Func1() { //Notify GUI via PostMessage to disable the controls //Do some processing //Notify GUI via PostMessage to enable the controls //return errorcode } The weird thing is that sometime it works fine and sometimes the GUI freezes. I am seeing the UI buttons getting disabled then performing the operation -then ui buttons getting enabled back again. The Gui freeze happens randomly after that and when it freezes the buttons become unclickable and i am not receiving any message in PreTranslateMsg() after the freeze for mouse click or anything else

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

                Zishan Ud787 wrote:

                case BUTTON1:

                This case is not ever going to execute because it's all uppercase (and what's getting assigned to m_threadCmd is not). Is that intentional?

                "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

                "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                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