[gtk-win32] g_idle_add() weirdness



I've written a small test app which just consists of a dialog with a button
and a progress bar.  When the user presses the button I want a worker thread
to update the progress bar periodically.  I'm experienced enough to know
that it's not a good idea to modify GUI elements from a worker thread
because the app tends to crash if (say) the user moves or resizes the main
window whilst the worker thread is running.  Under MFC I would solve this
problem by sending messages but I wasn't sure how to tackle it under GTK.

Someone suggested that I update the GUI by means of idle processing, using
'g_idle_add()'.  In other words, I set up a function within the worker
thread to receive periodic notifications.  That function then calls
g_idle_add() to schedule another function which updates the progress bar
(theoretically within the GUI thread's idle time).  Here's how it looks:-

pthread_t UIthreadID;   // Global - gets set elsewhere in the program
pthread_t WTthreadID;   // Global - gets set elsewhere in the program
pthread_t thisThreadID;   // Global - for comparison purposes
gboolean MainWindow::idle_set_progress(gpointer _pValue)
{
   // In theory, this function should be executing
   // from within the GUI thread
   thisThreadID = pthread_self();
   if ((m_pProgress) && (_pValue))
   {
       double* pNewValue = static_cast<double*>(_pValue);
       double  new_value = *pNewValue;
       m_pProgress->set_fraction (new_value);
   }

   return (false);
}


// This is the other function.  It periodically receives signals emitted
// by the worker thread.  It runs as part of the worker thread.
void WorkerThreadNotification(double fraction) const
{
   // Add some idle processing for the main loop
   g_idle_add(MainWindow::idle_set_progress,
                            &currentProgressValue);
}

Notice that I've set up some 'threadID' variables so that I can check which
thread is executing idle_set_progress().  I've built the program under
Linux, Cygwin and Windows (each using their respective versions of GTK).
Under Linux and Cygwin, 'idle_set_progress()' does get executed in the GUI
thread, exactly as expected.  But under gtk-win32 it gets executed in the
worker thread.  Is this a known problem with the gtk-win32 build?  More
importantly....  is there a fix??

Thanks,

John

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]