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.
What am I missing? When the workerFunction is not started in a thread, it works without problems.
I don't touch the GUI directly - only via wxPostevent().
when I don't do this, the app also crashes in Windows and earlier in Linux. I thought I have to use it, becasue in the documentation I found the following hint:
Yields control to pending messages in the windowing system. This can be useful, for example, when a time-consuming process writes to a text window. Without an occasional yield, the text window will not be updated properly, and on systems with cooperative multitasking, such as Windows 3.1 other processes will not respond.
mspoerr wrote:I thought I have to use it, becasue in the documentation I found the following hint:
Yields control to pending messages in the windowing system. This can be useful, for example, when a time-consuming process writes to a text window. Without an occasional yield, the text window will not be updated properly, and on systems with cooperative multitasking, such as Windows 3.1 other processes will not respond.
this applies when you have a long-running task in the main thread, then you can call Yield() to give the application the chance to process events and update its gui. In a secondary thread you don't need it (because the main thread is not blocked) and it's probably bad because it would start processing events in the secondary thread.
when I don't do this, the app also crashes in Windows and earlier in Linux.
this applies when you have a long-running task in the main thread, then you can call Yield() to give the application the chance to process events and update its gui. In a secondary thread you don't need it (because the main thread is not blocked) and it's probably bad because it would start processing events in the secondary thread.
oh -ok.
With Log Window I mean a text window, where the events from the thread are posted -> Thread runs and is very chatty. All output is sent via wxPostEvent to the Log Window, where it is displayed. What would be a good solution for this? For what do I need to take care of?
first you should find out if this is really the cause for the crash. Does everything work fine if you comment out the wxPostEvent call?
What kind of event do you send? A custom event or a standard wxCommandEvent? wxString is not thread-safe, so sending strings using wxCommandEvent::SetString is a potential reason for your crash.
But then again, these kind of crashes are usually more random, so it's possible that your problem is really somewhere else. Do you have a backtrace?
you should have posted that code earlier, this can't work at all. You're only writing the pointer &evtDat into the event data, the data itself is a local variable, so the pointer will be invalid when the event gets processed.
You're only writing the pointer &evtDat into the event data, the data itself is a local variable, so the pointer will be invalid when the event gets processed.
sounds reasonable
You should create a custom event for this purpose.
you need an own event class if you want to send more complex data. For that it's important to overwrite wxEvent::Clone() with your own version that makes sure all the data is copied correctly.
Stupid question: How to use the GetData() function? Would you please so kind to post a sample?
I always get an compiler error: 'conversion' : cannot convert from 'boost::any' to 'std::string'
// Option 1
std::string str = event.GetData<std::string>();
// Option 2
boost::any data = event.GetData();
std::string str = boost::any_cast<std::string>(data);
// When you need to check the type of the data you can use typeid:
if (data.type() == typeid(std::string)) // ...