Re: interrupting a big loop



Allin Cottrell wrote:
[...]
Thank you, this is just the sort of thing I was groping for.  I have
one more question.


    /* It is possible that your callback
     * `on_cancel_button_clicked()' was called
     * during `gtk_main_iteration()'.
     */
    if (should_stop == TRUE) break;


What's the best way of implementing this?  Make "should_stop" a global
boolean that is initially set to FALSE, and is set TRUE within the
cancel button callback?  Or can one avoid a global?

You could beat around the bush a little, like this:

on_cancel_button_clicked(cancel_button, ... data) {
    gboolean *should_stop = data;
    if (should_stop)
        *should_stop = TRUE;
}

expensive_callback(some_object... data) {
    gboolean should_stop;
    gulong        sigid;
    should_stop = FALSE;
    sigid = g_signal_connect(cancel_button, "clicked",
                             on_cancel_button_clicked, &should_stop);
    for (;;) {
        /* ...
         */
        if (*should_stop) break;
        /* ...
         */
    }
    g_signal_disconnect(cancel_button, sigid);
}


But that is a little meticulous wouldn't you say ?

Cheers,
                                   -Tristan





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