Re: Idle Function Not Getting Called



On 09/04/2009 08:12 PM, Marshall Lake wrote:

What stops an idle function from being executed? ... pending events, right?

I have a situation where an idle function does NOT get called with the
following code:

    g_idle_add ((GSourceFunc) idlefunc, NULL);

Well, that works fine for me, and I'm sure others as well.  A
fundamental thing like that being broken would likely create some noise.
 Remember that you have to return control to the main loop after adding
your idle function.  Gtk/Glib is single-threaded in normal use.

OR

    while (gtk_events_pending ())
        gtk_main_iteration ();
    g_idle_add ((GSourceFunc) idlefunc, NULL);

Well, that's not fundamentally different from the above.  Obviously it
won't run before you add it.  The addition of flushing the event queue
before adding it doesn't really do anything.

However, the idle function gets called fine with the following code:

    g_idle_add ((GSourceFunc) idlefunc, NULL);
    while (gtk_events_pending ())
        gtk_main_iteration ();


What would account for such behavior?

As I said, you probably aren't letting the main loop run after adding
your idle function.  The while loop there explicitly forces the main
loop to run, but it's generally not good practice unless you know what
you're doing.

Additionally, in the third code snippet, program flow gets stuck in the
while loop until the idle function removes itself by returning FALSE.

Yes, that's expected.  And idle function is *always* ready to run, and
so gtk_events_pending() will *always* return true if an idle function is
present.

        -brian



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