abc.cpp (catching event) (source):
Code: Select all
Connect(wxID_ANY, ID_KEY, wxCommandEventHandler(MainWindow::ChangeLetter), 0, this);
Code: Select all
Connect(wxID_ANY, ID_KEY, wxCommandEventHandler(MainWindow::ChangeLetter), 0, this);
...
void *MainWindow::KeyThread(void *arg) {
wxEvtHandler *obj = wxDynamicCast(arg, wxEvtHandler);
if (obj) {
wxSound sound(cur_sound);
sound.Play(wxSOUND_SYNC);
wxCommandEvent KeyEvent(ID_KEY, wxID_ANY);
wxPostEvent(obj, KeyEvent);
}
pthread_exit(NULL);
}
...
void MainWindow::ChangeLetter(wxCommandEvent& event) {
...
}
sound.cpp (source):
Code: Select all
/** cleans up for thread exit */
static void exitThreadEvent(void* arg) {
unloadChunks();
thread_is_active = false;
wxEvtHandler *source_window = wxDynamicCast(arg, wxEvtHandler);
if (source_window) {
// DEBUG:
logMessage("Sending SoundFinishEvent ...");
// event to send to main thread
wxCommandEvent SoundFinishEvent(wxEVT_NULL, ID_SOUNDEND);
wxPostEvent(source_window, SoundFinishEvent); // FIXME: event not caught
}
}
Code: Select all
Connect(ID_SOUNDEND, wxEVT_NULL, wxCommandEventHandler(MainWindow::OnSoundFinish), 0, this);
Code: Select all
void MainWindow::OnSoundFinish(wxCommandEvent& event) {
// DEBUG:
logMessage("SoundFinishEvent");
}
Whenever the method SoundPlayer::play is called, my MainWindow instance is passed as a parameter. This passes the instance to a new thread which plays a sound so that an event can be sent back to it as the thread finishes (NOTE: My SoundPlayer class handles playing all sounds with SDL, previously I used wxSound but wanted support for OGG/Vorbis audio).
The reason I am using pthreads is because that is traditionally what I used & am familiar with it. I have thought about making the change to wxThread, & only just recently learned about std::thread. But this should work with pthreads.
I have been trying to figure out for about 24 hours now, why this isn't working. Can anyone see my mistake(s) in my code?