Maybe wxThread Problem? 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
ONeill
I live to help wx-kind
I live to help wx-kind
Posts: 152
Joined: Wed Oct 04, 2006 12:51 pm

Maybe wxThread Problem?

Post by ONeill »

Hello,

i've got a problem, and i think its caused by a wxThread class of mine.

I've got a normal wxFrame class, which starts a second class in a seprated wxThread.

Code: Select all

wxThread::ExitCode MyBarcodeReader::Entry()
{
	char* receivedString = NULL;
    size_t readedBytes = 0;

	//Warte auf den ersten string
	int state = comDevice->ReadUntilEOS(receivedString,&readedBytes,"\r",timeout);
	if(readedBytes > 0) {
			receivedString[readedBytes] = 0;
			((MyFrame*)frame)->addArtikel(receivedString);
	}

	//Solange kein Timeout passiert, weiter abfragen
	while(state == 1) {
		state = comDevice->ReadUntilEOS(receivedString,&readedBytes,"\r");
		if(readedBytes > 0) {
			receivedString[readedBytes] = 0;
			((MyFrame*)frame)->addArtikel(receivedString);
		}
	}

	//Port wieder schließen
	comDevice->Close();

	//Scanner Status ändern
	((MyFrame*)frame)->setScannerState(false);

	return 0;
}
This Thread looks if a barcode scanner sends data over the COM port.

If it does, the class calls a method in my Frame class to add it to a wxGrid ( the barcode ). This happens in addArtikel:

Code: Select all

void MyFrame::addArtikel(std::string artikel)
{
		//String aufspalten
		std::string sn = artikel.substr(0,artikel.find("|"));
		std::string kn = artikel.substr(artikel.find("|")+1,artikel.size());


		//Neues Tab erstellen, Grid anlegen und Daten einfügen
		
		std::map<std::string,wxGrid*>::iterator it = kundendaten.insert(std::make_pair(kn,new wxGrid(notebook, wxID_ANY))).first;
		it->second->CreateGrid(0,1,wxGrid::wxGridSelectRows);
		it->second->SetColLabelValue(0,"Artikel");
		it->second->SetColSize(0,200);
		it->second->AppendRows(1);
		it->second->SetCellValue(it->second->GetNumberRows()-1,0,sn);
		notebook->AddPage(it->second, kn, true, 0);
	}
I want to create a new wxGrid control and add it to the notebook page and include the barcode.

The page is added but the wxGrid doesnt show up. For about a second there are some grey fields and then they disapear and there is only the tab of the notebook ctrl is left.

Could this be a Thread problem, because if i create the tab and the wxGrid in another method of my wxFrame class, it works.

Thanks for help
spectrum
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 207
Joined: Sat Jul 21, 2007 12:17 pm

Post by spectrum »

Hello ONeill,

this is a well known problem.

You cannot update any part of the GUI from a thread.

I personally use a custom (non wx) global Queue class to post event to the main interface, i read the queue from the wxFrame::OnIdle().

Otherwise, there is some wxWidgets support for this:

http://wiki.wxwidgets.org/Inter-Thread_ ... munication

look at wxMessageQueue

greetings
spectrum
ONeill
I live to help wx-kind
I live to help wx-kind
Posts: 152
Joined: Wed Oct 04, 2006 12:51 pm

Post by ONeill »

Thanks.

I thought that it could be this problem. It's not only a wxWidgets problem. :)

Thanks for your link and hint. I will look at it!
ONeill
I live to help wx-kind
I live to help wx-kind
Posts: 152
Joined: Wed Oct 04, 2006 12:51 pm

Post by ONeill »

It will work for me, if I simply send an event from the worker class to my frame.

I used the code from the site you posted, but there is a problem with the declaration and definition of the event.

I've got two files. worker.h/cpp and main.h/cpp
I used the following code to define the event:

Code: Select all

// Neuen Event Type deklarieren
DECLARE_EVENT_TYPE(wxEVT_THREAD, -1)
 
// Neuen Event Type definieren
DEFINE_EVENT_TYPE(wxEVT_THREAD)
I wrote this code in header of my worker file, then i included the file into the header of the main file. But then i get 3 link errors, because the event is allready declared.

How can i solve that problem?
JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway »

Hi,

This part needs to be in one of your .cpp files, only defined once:

Code: Select all

// Neuen Event Type definieren
DEFINE_EVENT_TYPE(wxEVT_THREAD) 
Jim
OS: Vista SP1, wxWidgets 2.8.7.
Post Reply