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?
Is it possible to SEND an event? Topic is solved
Every wxWindow is derived from wxEvtHandler. You can simply do for example:
Any event can be injected in the event queue. Really neat!
- Jorgen
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);
}
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
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.
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.
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:
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;
It's one of many solutions.
Regards,
- Jorgen
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
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
}
Regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb