[Glade-users] uni- versus multi-threaded gtk



Thank you for clarifying, Alexey. Most of my coding has been very
"old-school" stuff, and most of my experience with event loops was in
X-Windows. Your recommendation makes more sense!
-- 
Mannex



On Sat, 2011-06-25 at 22:48 -0500, Alexey wrote:

On Sat, 2011-06-25 at 11:01 -0700, David Buchan wrote:
// main calculation loop
while (flag == 0) {
  make some heavy duty calculations;

// Check if the user has clicked any buttons (e.g., the STOP button)
    while (gtk_events_pending()) {
      gtk_main_iteration();      // let the main gtk loop deal with it
    }
  carry on with more ridiculously heavy duty calculations;
}

The way I set it up, if the user clicks the STOP button, a callback is
executed which sets the flag to zero (flag = 0). The main calculation
while-loop then ends peacefully.

It's not the preferred way to do it. The preferred way is to replace
your loop with an idle function which is called repeatedly whenever
there's no other events are to be processed. You use g_idle_add() to add
the function to the main loop and the main loop takes care of all the
necessary checks. All you have to do in the function is returning TRUE
until the job is done and finally return FALSE to stop the function
being called. To cancel the job by the user request you'd use
g_source_remove() in the appropriate callback.

The approach you described is acceptable only for porting some old code
relying on polling user input within some complex loop which is too
burdensome to untangle. In general, unless you do some low level stuff,
if you feel the need to poll for events you probably doing something
wrong, cause it is the main loop job to decide when and what to process.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/glade-users/attachments/20110625/50b5c1c1/attachment.html 




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