Block signal to avoid callback infinite loop

Hi,
I have a GtkSwitch, with a connected callback for the “state-set” signal.
In the callback function, I sometimes need to reverse the state of the switch, to indicate that something went wrong, and could not be switched on. And here is my problem: how can I set the state of the GtkSwitch from within the callback, with gtk_switch_set_active(), without triggering another call to the callback function?
I tried:

  g_signal_handler_block(self, handler_id);
  gtk_switch_set_active(self, state);
  g_signal_handler_unblock(self, handler_id);

but, it doesn’t work. Probably, when the signal is emitted, it is already unblocked.
Thank you!

The approach looks good, but GtkSwitch has probably some peculiarities.

Sometimes storing a boolean variable saying “I’m doing this operation” is necessary.

But I would test first the different GtkSwitch possibilities (signals and properties notifications, and connecting the relevant signals with the after flag or not). And thoroughly read the GtkSwitch docs (which you have probably already done :wink: )

1 Like