Calling hide method of Fl_Double_Window has no effect in a multi-thread FLTK C++ application (Visual Studio 2010)

StackOverflow https://stackoverflow.com/questions/17029625

Question

I'm working on a C++ FLTK application. This is a multi-thread application that creates & shows a modal window in a thread using below code:

Fl_Double_Window* dlg = new Fl_Double_Window(0, 0, 200, 100);
...
dlg->set_modal();
Fl::visual(FL_DOUBLE|FL_INDEX);
dlg->show();

then in same thread I create a new thread & pass pointer to Fl_Double_Window object to it as thread parameter:

CreateThread(
        NULL,                   // default security attributes
        0,                      // use default stack size  
        &beginProgress,     // thread function name
        (LPVOID) dlg,       // argument to thread function 
        0,                      // use default creation flags 
        NULL);

& in my thread function I do some operations & then I need to hide the showing modal window:

DWORD WINAPI beginProgress(LPVOID args)
{
    //do some operations
    ((Fl_Double_Window*)args)->hide();
    return 0;
}

the problem is here that my code executes successfully with no error, but after executing hide method of dlg object pointer, window does NOT hides & seem calling hide or even deleting window object using delete dlg has no effect.
I guess this problem is related to multi-threading behavior, but I can't guess what cause the problem & how should I solve it.

Was it helpful?

Solution

Please read the FLTK guide on multithreading:

FLTK supports multiple platforms, some of them which do not allow any other but the main thread to handle system events and open or close windows. The safe thing to do is to adhere to the following rules for threads on all operating systems:

  • Don't show() or hide() anything that contains widgets derived from Fl_Window, including dialogs, file choosers, subwindows or Fl_GL_Windows

Here is the FLTK-recommended way to handle GUI updates from a background thread:

The only workaround is to store what you want to do in static variables, call fltk::awake(), and make the main thread call fltk::wait() repeatedly, checking and acting on the static values after each call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top