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

How to stop this loop that was initiated by a buttonclick?



I'm having the following basic question that is puzzling my mind:
Suppose i have a button that when it is being clicked it starts a loop. 
Let's take an infinite loop in my example.  Clicking the button starts
the loop.  Suppose there's only one way to stop the loop, and that is
setting a variabele i to zero.  Suppose that setting i to zero can only
be done by a second button.  Now my problem is:

I can't click the second button because the first button starts an
infinite loop and i can't click any buttons anymore until that loop is
finished... the problem is that the loop can only be finished by pressin
button two, but that is never possible because of the infinite loop of
button one!

How do i stop the loop from button1 with a click on button2.  I am
encountering this kind of problem in an application where i want to
control a steppermotor.  One button click starts a loop that sends bits
to the parallel port and another button should set some kind of
variabele so that the motor can be STOPPED.

Can somebody help me out?  I wrote a little 'demo-application' to
demonstrate my problem.

Thanks a lot for helping me out!


This is the demo program:


#include <gtk/gtk.h>

void loop(GtkButton *button, gpointer user_data)	/* How should this
function look like? */
{
 gint i=1;
 
 while (i==1) {
   g_print("Still looping\n");
   }
}
   
void stop(GtkButton *button, gpointer user_data)	/* And what about this
one that should stop */
{							/* the looping ??? */
 g_print("STOP!\n");
}

int main (int argc, char *argv[])
{
 GtkWidget	*window;
 GtkWidget 	*button1;
 GtkWidget 	*button2;
 GtkWidget	*hbox;
 
 gtk_init(&argc, &argv);
 
 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 hbox = gtk_hbox_new(TRUE, 2);
 button1 = gtk_button_new_with_label("Start looping");
 button2 = gtk_button_new_with_label("Stop looping");
 
 gtk_container_set_border_width(GTK_CONTAINER (window), 10);
 gtk_box_pack_start(GTK_BOX (hbox), button1, TRUE, TRUE, 1);
 gtk_box_pack_start(GTK_BOX (hbox), button2, TRUE, TRUE, 1);
 
 gtk_container_add(GTK_CONTAINER (window), hbox);
 
 gtk_signal_connect(GTK_OBJECT(button1), "clicked", loop, NULL);
 gtk_signal_connect(GTK_OBJECT(button2), "clicked", stop, NULL);
  
 gtk_widget_show(button1);
 gtk_widget_show(button2);
 gtk_widget_show(hbox);
 gtk_widget_show(window);
 
 gtk_main();
 exit(0);
}

-- 
Bart Vandewoestyne		http://hello.to/MC303		
Hugo Verrieststraat 48		Bart.Vandewoestyne@skynet.be
8550 ZWEVEGEM			ICQ# 21275340
BELGIUM - EUROPE		nick: MC303
Phone: +32(0)56/75.48.11
-------------------------------------------------------------
"If we knew what it was we were doing, it would not
 be called research, would it?"
				-- Albert Einstein --



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