Re: interrupting a big loop



From: "Allin Cottrell" <cottrell wfu edu>:
When my (gtk) app is engaged in a time-consuming loop process, I'd
like to offer the user the chance of breaking out if he/she loses
interest.  The pseudo-code is something like:

for (i=0; i<LARGE_NUMBER; i++) {
   if (break_condition()) break;
   /* do something complicated */
}

where "break_condition()" checks whether a particular quit key has
been pressed or quit button has been clicked.
Hi Allin!
You should show a progressbar and an Abort-button to inform the user about
the progress of your operation and that he can abort it. Then you could do
something like this:
int flExit = 0;
for (i=0; !flExit && i<LARGE_NUMBER; i++) {
  while (gtk_events_pending()) gtk_main_iteration();
  /* do something complicated */
}

In the callback of the Abort-button simply set flExit to 1. This also has
the advantage that your UI gets updated when the user tries to move/resize
the window or when it is exposed.

Regards,
                    Peter




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