Re: [gtk-list] self-removing timeouts



On Sun, 29 Mar 1998, Havoc Pennington wrote:

> 
> Hi,
> 
> Hoping someone can enlighten me on timeouts a little. 
> 
> It seems that I can't remove a timeout from within the timeout:
> 
> #include <gtk--.h>
> #include <iostream.h>
> 
> gint timeout;
> 
> gint iteration(gpointer data)
> {
>   static int num_iterations = 0;
> 
>   cerr << "Iteration...";
>   ++num_iterations;
> 
>   if (num_iterations == 15) {
>     cerr << "Stopping...";
>     ((Gtk_Main *)data)->timeout_remove(timeout);
>     // return false;  // this would work	
>   }
>   return true;
> }
> 
> int main(int argc, char ** argv)
> {
>   Gtk_Main m(&argc, &argv);
>   timeout = m.timeout_add(100, iteration, &m);
>   m.run();
> }
> 
> This just keeps on going forever, without so much as a
> warning. Timeout removal works in testgtk, and the above program can
> be made to work with a "return false," I think.

this is correct. a timeout function (or idle or what else) can be removed
by a call to gtk_timout_remove() only if it is not currently invoked.
basically the implementation for timeout functions is as follows:

gtk_timout_add()	-> add a new functiuon to the timeout_list.
gtk_timeout_remove()	-> remove a function from the timeout_list.
gtk_main_iteration_do()	-> ..., check for timouts that have expired
			   and invoke them.
invokation of a timeout:
- remove the timout from timeout_list.
- invoke the timout function.
- if the timout function returned TRUE, add it back to timeout_list.

so at the moment of the timout's invokation, it's tag can't be removed
from timeout_list, since it is not there anymore.
a solution to your specific problem might be to add the timout function
yourself inside of the tiomout function, and let it return false always.
then, if neccessary, the tag can be removed.

> However, in my program the timeout sometimes causes a signal to be
> emitted which calls a function to remove the timeout, so the return
> false would have to happen via some roundabout mechanism.
> 
> Is returning false the only way to remove a timeout from within a
> timeout? Is timeout_remove() explicitly disallowed from inside the
> timeout? The tutorial makes it sound like "return false" is a
> convenience thing and never required.

well, the tutorial might need to be updated in that respect, anyways
since the timout function is not in the timout_list upon invokation
anymore, since it will be re-added once you return TRUE, gtk_timout_remove()
is not explicitely disallowed from within timout functions, but it will
not prevent re-additions of timouts because of a return value of TRUE.

> 
> Thanks,
> 
> Havoc Pennington
> http://pobox.com/~hp
> 

---
ciaoTJ



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