2 Replies - 354 Views - Last Post: 21 May 2012 - 09:33 AM Rate Topic: -----

#1 juryben  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 74
  • Joined: 30-November 10

Calling a Class Function from a Thread

Posted 20 May 2012 - 10:00 PM

Hey boys, I'm starting to build GUIs with wxWidgets and I'm having a problem calling class functions from a thread.

To visualize:

              Parent Thread
               /          \
          GUI Thread     Worker Thread



All my parent thread does is spawn the gui thread and a worker thread.

Now I'm my GUI thread I have a class like so:

GUI.h
class Listbox : public wxFrame
{
public:
	Listbox(const wxString& title);

	wxListBox *listbox;

	Listbox *lb;

	void AddToList();
};



GUI.cpp
Listbox::Listbox(const wxString& title)
	: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 500))
{
	wxPanel * panel = new wxPanel(this, -1);

	wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);

	listbox = new wxListBox(panel, ID_LISTBOX,
	wxPoint(-1, -1), wxSize(100, 100));

	hbox->Add(listbox, 3, wxEXPAND | wxALL, 20);

	panel->SetSizer(hbox);
	Center();

	AddToList();
}

void Listbox::AddToList()
{
	listbox->Append("AddedThisText");
}



Now that code works.

My problem is I want to call the function AddToList from my worker thread. But this fails:

int worker()
{
	if(...)
	{
		Listbox *obj;
		obj->AddToList();
	}
	return 0;
}



Why is it failing? I get this error from the debugger.

Posted Image

This post has been edited by juryben: 20 May 2012 - 10:03 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Calling a Class Function from a Thread

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6079
  • View blog
  • Posts: 23,550
  • Joined: 23-August 08

Re: Calling a Class Function from a Thread

Posted 21 May 2012 - 02:29 AM

It's not thread-related, but simply because obj doesn't point to anything.
You're going to need to pass a Listbox object to your thread function anyway.
Was This Post Helpful? 1
  • +
  • -

#3 juryben  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 74
  • Joined: 30-November 10

Re: Calling a Class Function from a Thread

Posted 21 May 2012 - 09:33 AM

View PostJackOfAllTrades, on 21 May 2012 - 02:29 AM, said:

It's not thread-related, but simply because obj doesn't point to anything.
You're going to need to pass a Listbox object to your thread function anyway.

Thanks man. Solved it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1