Page 2 of 2

Re: Custom Event Isn't Being Caught

Posted: Wed Jul 03, 2019 1:14 pm
by evstevemd
Deluge wrote: Wed Jul 03, 2019 11:18 am Thank you, but that is old code, & none of that helps me with the question of this topic.

--- Edit ---

I only linked to my old code to show the examples of how posting custom events was working.
Can you make simple compilable app which demonstrates your problem. That way one can easily test and see the issue. Linking old code does not help and is waste of time for people who try to help!

Re: Custom Event Isn't Being Caught

Posted: Wed Jul 03, 2019 2:12 pm
by ONEEYEMAN
Hi,
But in the beginning you said that you moved a class to a new file and it is not residing in the same file anymore.
Which means it will create 2 id's every time the program run.

Thank you.

Re: Custom Event Isn't Being Caught

Posted: Wed Jul 03, 2019 4:13 pm
by doublemax
According to the docs, wxEventType is the first argument, & id is the second.
Sorry about that, i only looked at wxEvent and wrongfully assumed the order would be the same in wxCommandEvent (WTF wx?).

After reading your code, the most likely reason for the failure is what alys666 wrote: The definitions of IDs in a header file is a no-go because it will get loaded multiple times and a different ID for ID_SOUNDEND (and all others) will be created.

For a test, change the ID in the Connect call to wxID_ANY, so that it matches any ID.

Re: Custom Event Isn't Being Caught

Posted: Wed Jul 03, 2019 8:39 pm
by Deluge
evstevemd wrote: Wed Jul 03, 2019 1:14 pmCan you make simple compilable app which demonstrates your problem. That way one can easily test and see the issue. Linking old code does not help and is waste of time for people who try to help!
Only the first link was to my old code. I'm using Git version control hosting on GitHub. So to see the full current code, just go to https://github.com/AntumDeluge/myabcs. My first links were to previous commits that demonstrated how the program had been working correctly in the past. The links after that were to the code as it is (or as it was at the time).
doublemax wrote: Wed Jul 03, 2019 4:13 pmAfter reading your code, the most likely reason for the failure is what alys666 wrote: The definitions of IDs in a header file is a no-go because it will get loaded multiple times and a different ID for ID_SOUNDEND (and all others) will be created.
I understand. I thought that making it static const fixed that, but I see now that it does not. I will switch to using an enum or defining the values in a .cpp file.

However, this is my current code that does not use an ID (or rather, it implicitly uses wxID_ANY):

sound.cpp:

Code: Select all

// event to send to main thread
wxCommandEvent SoundFinishEvent(EVT_SOUND_FINISH);
wxPostEvent(source_window, SoundFinishEvent); // FIXME: event not caught
abc.cpp:

Code: Select all

// sounds finish playing
Bind(EVT_SOUND_FINISH, &MainWindow::OnSoundFinish, this);
Event is still not being caught.....

.....But, as I write this, I think I realize why... and it IS related to what alys666 was saying. I created the new event type in a header using wxNewEventType:

Code: Select all

static const wxEventType EVT_SOUND_FINISH = wxNewEventType();

So two different event types are created. sound.cpp is sending one type, & abc.cpp is waiting for another. I will post back after I fix this.

--- Edit ---

Yep, that was the problem :). New event code:

event.h:

Code: Select all

#ifndef MYABCS_EVENT_H
#define MYABCS_EVENT_H

#include <wx/event.h>


extern const wxEventType EVT_SOUND_FINISH;


#endif /* MYABCS_EVENT_H */
event.cpp:

Code: Select all

#include "event.h"


const wxEventType EVT_SOUND_FINISH = wxNewEventType();
Thank you alys666 & doublemax, problem solved.

--- Edit ---

Initially, I marked my post as the correct answer. But I'm going to mark alys666's as such, because I think that would have solved the problem at the time. I wasn't creating a new event type then, but rather using the ID to catch the event. So declaring the ID as extern & defining in a separate .cpp file would have fixed it. Thank you alys666.

--- Edit ---

For reference, here is the new ID code:

id.h:

Code: Select all

#ifndef MYABCS_ID_H
#define MYABCS_ID_H

#include <wx/utils.h> // wxNewId();


extern const int ID_EXIT;
extern const int ID_ABC;
extern const int ID_FOOD;
extern const int ID_ANIMALS;
extern const int ID_MUSIC;
extern const int ID_TOYS;
extern const int ID_HELP;
extern const int ID_ABOUT;
extern const int ID_SPACE; // Id used in threads for space
//extern const int ID_TAB; // Id used in threads for tab
extern const int ID_KEY; // Id used in threads for keypress
extern const int ID_OTHER; // Id used in other threads


extern const int ID_WINDOW;
extern const int ID_BG;


extern const int ID_CHANGELOG;
extern const int ID_LOG;


#endif /* MYABCS_ID_H */
id.cpp:

Code: Select all

#include "id.h"


const int ID_EXIT = wxNewId();
const int ID_ABC = wxNewId();
const int ID_FOOD = wxNewId();
const int ID_ANIMALS = wxNewId();
const int ID_MUSIC = wxNewId();
const int ID_TOYS = wxNewId();
const int ID_HELP = wxNewId();
const int ID_ABOUT = wxNewId();
const int ID_SPACE = wxNewId(); // Id used in threads for space
//const int ID_TAB = wxNewId(); // Id used in threads for tab
const int ID_KEY = wxNewId(); // Id used in threads for keypress
const int ID_OTHER = wxNewId(); // Id used in other threads

const int ID_WINDOW = wxNewId();
const int ID_BG = wxNewId();

const int ID_CHANGELOG = wxNewId();
const int ID_LOG = wxNewId();

Re: Custom Event Isn't Being Caught

Posted: Wed Jul 03, 2019 11:44 pm
by alys666
a bit of code mentoring
1. do not use this C-style header protection -

Code: Select all

#ifndef MYABCS_ID_H
#define MYABCS_ID_H
...
#endif
use everywhere:

Code: Select all

#pragma once
at the beginning

2. to decrease header dependencies, do not include excessive headers in your header file, include only needed for declarations in this header.

3. do not use wxNewId, if you're not a freak. Define your tags/IDs as enumerations.
look how clean will your header be, and you will be able to get rid of wxNewId() assignment code to your IDs.
enums in C/C++ are not just enums as in pascal for example, but a regular way to declare integer numeric constants.
advantages of enum here
1. value is known during compilation, so:
a. compiler generates more effective code
b. program starts and runs faster
c. you can use switch statement with cases of this IDs, and other language features(for example templates), where compiler must know value at compilation time.
d. you'll get rid of quite hard for be fixed, C++ specific error - undefined order of global variables initialization.

Code: Select all

#pragma once
#include <wx/defs.h>
enum{
	ID_EXIT = wxID_HIGHEST + 1,
	ID_ABC,
	ID_FOOD,
	ID_ANIMALS,
	ID_MUSIC,
	ID_TOYS,
	ID_HELP,
	ID_ABOUT,
	ID_SPACE, // Id used in threads for space
	ID_TAB, // Id used in threads for tab
	ID_KEY, // Id used in threads for keypress
	ID_OTHER, // Id used in other threads
	ID_WINDOW,
	ID_BG,
	ID_CHANGELOG,
	ID_LOG
};