GUI Freezes randomly /MFC /C++
-
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
-
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
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.
-
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.
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
-
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
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
-
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
Hi @jeron1 , Thanks for pointing it out. Updated the question there as well .
-
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.
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.
-
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
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