« Return to Thread: Recalculation of timeouts with g_timeout_add

Re: Recalculation of timeouts with g_timeout_add

by Pascal Bonfils :: Rate this Message:

Reply to Author | View in Thread

I haven't tried to pass the timeout value but what do is with a global
variable.

Imagine that you have the reference of the timeout, as a global variable :
    guint TimeOut_Ref;

then you have a first call to your callback function
    TimeOut_Ref = g_timeout_add (20*1000, callback, (gpointer)&timeout);

you stop the timeout just when you enter the function :
    gboolean callback (gpointer data) {
        if (TimeOut_Ref > 0) {
            g_source_remove(TimeOut_Ref);
            TimeOut_Ref = 0;
            }
        ....
Then, at the end of you called function, just before the return, you
reactivate the timeout :
        .....
        TimeOut_Ref = g_timeout_add (20*1000, callback, (gpointer)&timeout);
         return TRUE;
    }


Regards

Pascal


Mitko Haralanov a écrit :

> I have a question about g_timeout_add's timeout recalculation.
>
> Currently, the next call time is calculated based on <time of callback
> thread start> + <timeout given to g_timeout_add>. This means that if I
> call g_timeout_add with a timeout of 20*1000 msecs (20 seconds) and my
> callback takes 10 seconds to complete, my callback will be called 10
> seconds after it completes. Here is an example:
>
> #include <gtk/gtk.h>
> #include <glib.h>
> #include <time.h>
> #include <stdio.h>
>
> gboolean callback (gpointer data) {
>      int timeout = *((int *)data);
>      printf ("callback called at: %lu\n", time (NULL));
>      sleep (timeout);
>      printf ("callback returns at: %lu\n", time (NULL));
>      return TRUE;
> }
>
> int main (int argc, char **argv) {
>      int timeout = 10;
>      gtk_init (&argc, &argv);
>      g_timeout_add (20*1000, callback, (gpointer)&timeout);
>      gtk_main ();
> }
>
>
> will produce this:
> callback called at: 1210893003
> callback returns at: 1210893013
> callback called at: 1210893023
> callback returns at: 1210893033
> callback called at: 1210893043
>
> What I would to happen is that my callback is called 20 seconds after
> it returns, not 20 seconds after it is started.
>
> How can I accomplish this?
>
>  

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list@...
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

 « Return to Thread: Recalculation of timeouts with g_timeout_add

LightInTheBox - Buy quality products at wholesale price