Hello,
I face a big problem in my program to update the text in a BStringView inside a function executed from a thread or a signal alarm.
This function is to display a countdown for a timeout while the application can be accessed to write text or click on the buttons. So this countdown is not a sleep() that freezes the application.
It’s like an exam to continue to the next question if the student does not reply to the current question fast enough.
Here is the structure I use:
const uint32 kAnswer=‘btc1’;
class MyClass : public BWindow {
private:
BStringView *Countdown;
public:
MyClass(BRect frame)
: BWindow(frame,"",B_TITLED_WINDOW,B_BORDERED_WINDOW_LOOK) {
Archive(new BMessage(kAnswer),true);
Countdown = new BStringView("","");
//…
}
public:
void MyFunction() {
std::string txt=Countdown->Text();
std::cout<<"Hello world!";
Countdown->SetText("000"); // => The application crashes here!!!
}
void MessageReceived(BMessage *message) {
switch (message->what) {
case B_KEY_DOWN:
std::thread t = std::thread(&MyClass::MyFunction,this);
t.detach();
break;
}
}
};
The function “MyFunction” is started successfully and displays “Hello world!”, but as soon as I execute Countdown→SetText(…), the application crashes.
I checked, “Countdown” is not NULL.
I already tried with a signal alarm:
MyClass *MC;
void on_alarm(int signal) {
if(alarm_stop) return;
else alarm(alarm_period);
MC->MyFunction();
alarm(1);
}
int main() {
MC = new MyClass(BRect(50,50,248,238));
signal(SIGALRM,on_alarm);
alarm(1);
//…
}
But it’s the same, it crashes when executing Countdown→SetText(…).
It’s very strange because Countdown→Text() works fine in “MyFunction”.
So I can read the text but I cannot change it from the function.
So please, do you have any idea about this issue or another workaround to create a background process with a signal every seconds to decrease a countdown in a function while continuing to access the application in parallel?
In advance thank you very much for your help.
Best Regards,