LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   interface not responding in glade 3 / gtk with C (https://www.linuxquestions.org/questions/linux-newbie-8/interface-not-responding-in-glade-3-gtk-with-c-861692/)

axd 02-09-2011 03:38 PM

interface not responding in glade 3 / gtk with C
 
Hi,

I am a fairly new ubuntu user. I am using ubuntu 10.04 and glade 3 for building a simple interface that uses C .

In my interface , I have created two buttons : start and stop .Tofocus on my actual problem , I have created a simpler UI and code .

start button runs a while(1) loop and stop should stop the loop .

The problem is , when I press start , the gui freezes and I cannot press any other button . I cant even close my app.After some thorough experimentation, I found out that when I press start , it never comes out of the while loop .

I need the loop to keep running until I press stop . What should i do ?

Here are the functions for the clickable buttons :

Code:


void
on_start_button_clicked (GtkButton *button, gpointer user_data)
{

        flag1=0;
       
        while(1)
        {

                printf("**********checking !!!! ********\n\n");
               
                if(flag1 == 1)
                                break;
        }
}


void
on_stop_button_clicked (GtkButton *button, gpointer user_data)
{
 
        flag1=1;       

}


schneidz 02-10-2011 08:32 AM

^ i dont know about the freezing issue (try putting in something like sleep(1); after the pprintf()) but the flag1 variable is not global.
what that means is that on_start_button_clicked's flag1 is not the same as on_stop_button_clicked's flag1.

axd 02-10-2011 09:02 AM

Quote:

Originally Posted by schneidz (Post 4254130)
^ i dont know about the freezing issue (try putting in something like sleep(1); after the pprintf()) but the flag1 variable is not global.
what that means is that on_start_button_clicked's flag1 is not the same as on_stop_button_clicked's flag1.

Thanks for the reply schneidz .

The flag1 variable is in fact global . When i click stop before I click start , the loop runs once and exits (that would only happen if the value of flag1 changed to 1 before i pressed start).

I also tried putting sleep() , but the loop would only free up my gui on its exit condition being satisfied.

If I replace the while(1) with :

Code:


int c=0;

while(c<100)
{

  printf("*********checking!!!!*******");
 
  if(flag1==1) break;
  c++;

}


the app would still run through the whole while loop ... I cannot press any other button till then .

schneidz 02-10-2011 12:09 PM

Quote:

Originally Posted by axd (Post 4254166)
The flag1 variable is in fact global . When i click stop before I click start , the loop runs once and exits (that would only happen if the value of flag1 changed to 1 before i pressed start)...

i find it weird that on_start_button_clicked sets flag1=0 before the while loop so it should never break out of the while.

maybe stepping thru the program in gdb mite help ?

edit: i dont think it is good programming behaviour to use breaks;

maybe something like this would be better:
Code:

void on_start_button_clicked (GtkButton *button, gpointer user_data)
{
        bool flag1=0;
        while(flag1 == 0)
        {
                printf("**********checking !!!! ********flag1 = %b\n\n",flag1);
        }
}

is it possible on_start_button_clicked could return a pointer of a bool instead of a void ?


All times are GMT -5. The time now is 02:26 AM.