Is it possible to SEND an event? 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
Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

Is it possible to SEND an event?

Post by Frank »

I have a thread, wich needs to get data from a window every second.

In WinAPI I would use SendMessage(), wich stops the execution of the thread until the window responds to the message.

But in wx I only find wxPostEvent(), wich is asynchron.

Is there a way in wx to get an event sent to a window from a thread and let the thread wait until the window responded?
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg »

Every wxWindow is derived from wxEvtHandler. You can simply do for example:

Code: Select all

	// minimize to tray when we left minimized
	// setting show to false does not work right now
	if(minimized)
	{
		wxIconizeEvent event(this->GetId(), true);
		this->AddPendingEvent(event);
	}
Any event can be injected in the event queue. Really neat!

- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

Post by Frank »

Yes, I know.

The problem is, when I'm doing this from a thread, the Event is executed in the context of the thread (I logged the Thread-IDs).

I wan't the mainthread to gather the data, to make sure, I'm not trying to get data from an item that the user is just deleting. And since wx is so picky with gui access from threads I don't want to do GUI Stuff in Threads.
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg »

Ok sorry I misread (or did not saw) part of your question.

So you want to post an event, wait for it to finish and continue .. hmm .. The difficulty lies in the fact your thread has to wait for the event to return. One solution is let the main thread post an event back to the thread.

In your thread define a function that acts as a mailbox. Like a fifo buffer. From your main thread, put a notification in there. Use wxMutex classes to shield access from the fifo when the thread is trying. In the thread do:

Code: Select all

   wxPostEvent( .. send event to main form .. );

   // within 2 sec, the mailbox will auto generate a response
   // to prevent hangups
   m_mailbox.SetResponseTimeout(2000);   

   // wait for response
   while(!m_mailbox.HasResponse())
      sleep(100);

   // now we continue
   if(m_mailbox.Aborted())
      .. handle your thread abort
   else
      .. handle response
The mailbox can be nothing more but an intelligent queue (wxArrayInt) with some wxMutex protection around critical sections. The auto timeout can be a system timer ticks counter so withint 2000 ms an auto message is posted to break the loop.

In the main thread;

Code: Select all

void MyFrame::OnThreadEventReceived(wxCommandEvent &event)
{
    MyThread *t = (MyThread *)event.GetEventObject();
    t->GetMailBox().PostMessage( .. reply that you got it .. );  // tiggers mailbox
}
It's one of many solutions.

Regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Frank
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 211
Joined: Sat Jan 01, 2005 6:19 pm

Post by Frank »

Sounds good. I'm trying it.

Thanks
Post Reply