Re: stop a procedure execution in GTK



On Monday 06 January 2003 02:00, 447158 celes unizar es wrote:

Hi,

  I'm programming an application using GTK+1.2.  I have to execute a
procedure (for instance as a callback when clicking on a "start" button)
and then I'd like to be able to stop it whenever the user gets bored (for
instance by having another button "stop").  

However I suppose that if a
callback procedure is executed it cannot stop and of course it would not
allow the application to catch new events till it's finished, or yes?

That's right: while you are in a callback procedure, the app does not process 
events.

However, what you can do is 

   /* computation going on */
   while (gtk_events_pending())
       gtk_main_iteration();
   /* computation continued */

(http://developer.gnome.org/doc/API/gtk/gtk-general.html#GTK-EVENTS-PENDING)

in your inner loop of the callback (where all the work is done). This way 
events will be processed if any occur, and widgets (e.g. a progress bar) will 
be refreshed.

One way to do what you want is to have a global variable calculation_stop, 
which you set to FALSE at the beginning of your callback. The stop button 
callback then simply sets this value to TRUE. Now you only need to regularly 
check this value in your 'work' callback procedure, and return if it is set.

One thing to look out for: make sure that the 'work' callback procedure cannot 
be entered twice (e.g. by disabling the 'start' button while you are in the 
'work' callback procedure, if this is the only way the procedure is called; 
or by using a static variable in the procedure that is set when the procedure 
is called the first time and unset when it is finished, so that you can exit 
immediately if the function is called a second time while you are already in 
that procedure).

Hope this helps

Cheers
-Tim





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