GTK: cancel timeout
Asked Answered
T

1

19

GTK allows you to set a timeout with g_timeout_add. Just like g_signal_connect, the g_timeout_add function returns an id representing the timeout. So, is there a way to cancel a timeout using the id? Looking through the documentation, I don't see any way to cancel a timeout event, but I would assume there must be some way, otherwise what is the point of the id value returned by g_timeout_add?

So, is there any way to cancel a timeout event, or is this just something that needs to be handled manually by setting a "cancellation flag" which can be checked within the user-provided timeout handler function?

Theron answered 14/8, 2012 at 15:4 Comment(0)
S
26

There are two ways to remove a callback registered through g_timeout_add():

  • Have the callback function return FALSE,
  • Call g_source_remove() with the identifier returned by g_timeout_add().
Scholiast answered 14/8, 2012 at 15:9 Comment(4)
is there anyway where I can cancel the call back before the first call. Ex: I schedule it after 10 secs and I decide to cancel it after 5 secs (its not yet called). After the first call back you will get identifier using which you can cancel but is it possible to cancel before that?Aksum
@Sagar, yes, with g_source_remove(). It takes the identifier returned by g_timeout_add(), so you don't have to wait for the callback to be invoked.Wargo
Oh i got it now. schedule func using g_timeout_add(), using the ret value from g_timeout_add(), you can cancel the call back invocation. Is my understanding is right?Aksum
For future googling: a message such as GLib-CRITICAL **: Source ID <id> was not found when attempting to remove it may indicate that g_source_remove() was called to remove a timeout handler that was not propely configured.Outherod

© 2022 - 2024 — McMap. All rights reserved.