1

When making UI updates with GTK# (GDK2), such as changing label text, setting button visibility etc, should I as a general rule be using Gdk.Threads.AddTimeout or Gdk.Threads.AddIdle?

The GDK2 documentation states that AddIdle

Adds a function to be called whenever there are no higher priority events pending.

So it would seem that AddTimeout would provide a more responsive update. However the documentation states:

Note that timeout functions may be delayed, due to the processing of other event sources. Thus they should not be relied on for precise timing.

Which leads me to believe Idle and Timeout are both about as responsive as each other (which they seem to be, when running code).

A comment in some code I am working on suggests that Timeout results in a faster update but hits the UI harder, but I cannot find any sources to back this up.

So as a general rule, which of these methods should I be using to perform GTK updates in the main thread?

Threads.AddTimeout(0, 0, () =>
    {
         // do something;
         return false;
    });

vs

Threads.AddIdle(0, () =>
    {
        // do something;
        return false;
    });
Owen Pauling
  • 11,349
  • 20
  • 53
  • 64

1 Answers1

2

The timeout callback is appropriate if you need something to happen after a specific period of time. An example of this would be blinking a cursor in a text field (not that you should implement that yourself).

Idle timeout gets called the once the main loop finishes executing everything else that is ready. The difference if you are just calling it once is priority. If you have some other event being handled by the main loop doing it in the idle handler guarantees that the other thing happens first.

The difference becomes more readily apparent when you return true, and get a repeated callback. If you do this in an idle callback you end up using as much CPU as the OS will let you, but your UI remains responsive assuming each callback is fast. If you do this with a timeout you get much more predictable behavior.

The only reason I would think this hits the UI harder is that it defaults to a higher priority, and could potentially delay draw events. Priority DEFAULT_IDLE < HIGH_IDLE (draw) < DEFAULT.

Blake
  • 368
  • 1
  • 11
  • Would you suggest that Idle be used by default for all callbacks that return false, like label text changes, button visibility etc, and Timeout only for special circumstances? – Owen Pauling Apr 28 '16 at 13:59
  • Probably. If I see a timeout in code my first guess as to why its there is because I want something to happen a specific amount of time in the future. If I want it done as fast as possible, but not before something in particular, I would expect to see someone use an idle callback with a priority the reflects how quickly to call back. – Blake Apr 28 '16 at 14:26