Re: GtkProgressBar state update issue



I'm sure that most new gtk programmers encounter this problem sooner or
leter.  I know I did and my solution was essentially the same as Tadej's.
I wish I seen his blog though as it would have saved me a lot of time!

The key to success is to ensure that GUI elements only ever get modified
within the main gtk GUI thread.  That can't happen if you call GUI functions
directly from some other thread because they'll execute in whichever thread
you called them from.  As Nikolaj said, that's really asking for trouble.

Fortunately, the GUI thread offers a solution.  Normally, it does whatever
you program it to do.  But the GUI thread offers you some "idle time" - a
place where you can put functions that (ideally) you'd like to call from
some other thread.  g_idle_add() and its cousins are the simplest way to
achieve this.

When you call one of the g_idle_add() functions, you're saying to gtk,
"don't execute this function now - but delay it until the GUI thread has
some idle time available and execute it there."  By doing that, you ensure
that GUI elements get updated in the right way and in the right place.

Don't underestimate the problems of trying to update GUI elements in an
inappropriate thread.  In your case, you were simply observing some
unfortunate behaviour.  But what you did could easily have caused your
program to crash.  Try moving your mouse very rapidly while your program is
running.  It's quite likely that it'll crash.  Mine did, though admittedly I
was running gtk under Windows which might not be the same as you.

John




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