wxButton - disable not working Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Ksawery
Earned a small fee
Earned a small fee
Posts: 22
Joined: Thu Jul 25, 2019 12:31 pm

wxButton - disable not working

Post by Ksawery » Mon Jul 29, 2019 2:38 pm

Hello,

I currently have two buttons in my main window, which become temporarily disabled (for 2 seconds) when pressed. However sometimes (but not always) when I press the buttons, they first "flash" (which I assume is the "button pressed" animation), and don't become disabled. Is this a bug? I'm not sure what could be causing it?

Here are the callback functions i'm using:

Code: Select all

/* "Wsun" button callback */
void cMain::OnButtonClickedWsun(wxCommandEvent& evt)
{
	m_btn_wsun->Disable();
	mbWriteMode = 1; //set flag, event handled in Modbus thread
	evt.Skip();
}

/* "Wysun" button callback */
void cMain::OnButtonClickedWysun(wxCommandEvent& evt)
{
	m_btn_wysun->Disable();
	mbWriteMode = -1; //set flag, event handled in Modbus thread
	evt.Skip();
}
Basically, every time a button it pressed, it will become disabled and a flag is set. Then the flag is handled in a helper thread, which also re-enables the button after 2 seconds. Sometimes (about 10-20% of the time), the buttons will not become disabled when pressed.

Here is the helper thread:

Code: Select all

/* Modbus helper thread */
wxThread::ExitCode cMain::Entry()
{
	while (!GetThread()->TestDestroy())
	{		
		{
			//Enter critical section
			wxCriticalSectionLocker lock(ModbusCS);

			if (mbWriteMode != 0) //writing to register, pause all other communication
			{
				if (mbWriteMode == 1)
				{
					mbWriteConfigStatus = -1; //modbus_write_register(ctx, 4001, 1);
				}
				else if (mbWriteMode == -1)
				{
					mbWriteConfigStatus = -1; // = modbus_write_register(ctx, 4001, 0);
				}

				if (mbWriteConfigStatus == -1)
				{
					m_static_txt_mberror_write->Show(); //show error
					wxThread::Sleep(1000);	//wait for 1s
					m_static_txt_mberror_write->Hide(); //hide error
					mbWriteConfigStatus = 0;
				}
				else
				{
					wxThread::Sleep(1000); //wait for 1s
				}

				if (mbWriteMode == 1)
				{
					m_btn_wsun->Enable();
				}
				else if (mbWriteMode == -1)
				{
					m_btn_wysun->Enable();
				}

				mbWriteMode = 0; //clear flag
			}
			else //continue reading input registers
			{
				mbReadADCErrorsStatus = modbus_read_input_registers(ctx, 3006, 1, &ADCErrors);
				wxThread::Sleep(20); //delay next poll by 20ms
				mbReadADCStatus = modbus_read_input_registers(ctx, 3001, 5, ADCReadings);
			}

			//Signal to main thread that new data has been received
			wxQueueEvent(GetEventHandler(), new wxThreadEvent());
		}

		//Delay next Modbus message cycle by 200ms
		wxThread::Sleep(200);
	}

	//Destroy thread
	return (wxThread::ExitCode)0;
}
Best regards,
Ksawery

Kvaz1r
Earned some good credits
Earned some good credits
Posts: 119
Joined: Tue Jun 07, 2016 1:07 pm

Re: wxButton - disable not working

Post by Kvaz1r » Mon Jul 29, 2019 3:30 pm

It hard to say without full reproducible code but you shouldn't call GUI function from worker thread at all, so it's almost certain the reason of behaviour in that.

Ksawery
Earned a small fee
Earned a small fee
Posts: 22
Joined: Thu Jul 25, 2019 12:31 pm

Re: wxButton - disable not working

Post by Ksawery » Mon Jul 29, 2019 3:47 pm

Hi, thanks for taking a look at my code. You are right, I totally forgot about that. I now moved the GUI code outside the thread and I think it's working correctly now.

Many thanks,
Ksawery

Post Reply