Re: Problems with Threading Concept

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

 



On Mon, 19 Feb 2007 12:44:45 +0530
naveen <naveen@xxxxxxxxxxxxxx> wrote:

>1) I have button to start some process (continuous).
>2) I need to stop that process, when i click the same button.
>
>to do the above tasks i developed a small application using threading
>concepts...

>But the problem here for me is, the stop is not happening immediatly
>after the click..
>
>Is there any mistake the way i am doning it ... or do i need to follow
>any other procedure for it.. please help me out...
>Naveen.

Hi,  the link that Andrew Cowie showed you, basically said that
you should use g_idle_add instead of the threads->enter and thread->leave
mechanism.

Your example was so simple, that you didn't even need a g_idle_add,
so I added a Label that gets updated from a thread through a g_idle_add.

I also changed your button click to a press, so the action occurs on
the first press instead of release, so it seems to respond faster.

Also note, if you add lines to print to the console, it takes alot of
cycles, and may cause the label update to sputter slightly at this high
speed.

Notice there there is no thread enter leave stuff in the following.
And I cheated a bit with global variables. :-)

/*  ********************************************************* /
/*  gcc -o test  test.c  `pkg-config --cflags --libs gtk+-2.0 gthread-2.0`   */

#include <gtk/gtk.h>
#include "stdio.h"
#include <pthread.h>

int flag=1,toggle=0;
int i=0;
pthread_t yes_tid; 
GtkWidget *label;
char ain[10];

gboolean update_label (gpointer in){
        sprintf(ain, "%d", in);
       gtk_label_set_text((GtkLabel *)label, ain ); 
 return FALSE;
}

void hello(){
       while(flag){
         i++;
      /* printf ("%d %d\n",i,flag); */ 
     /*  gtk_label_set_text((GtkLabel *)label, i); will segfault here  */
         /* use g_idle_add instead  */
         g_idle_add (update_label, i);
       }
return FALSE;
}

void hello_print( GtkWidget *widget, gpointer data ){  
   if(toggle==0){
        toggle=1;
        flag=1;
        pthread_create (&yes_tid, NULL,(void *)hello, NULL);
      }else{
          flag=0;
          toggle=0;
    }
}


int main(int argc,char *argv[] ){
    
    GtkWidget *window;
    GtkWidget *button;
    GtkWidget *Vbox;

    g_thread_init (NULL);
    gtk_init (&argc, &argv);

    gchar *text;
    PangoAttrList *attrs;
    gchar *str = 
    "<markup><span font_desc=\"Sans Italic 24\" foreground=\"black\" size=\"x-large\">    0    </span></markup>\0";

    /* Parsing the attributes to text*/
    pango_parse_markup (str, -1,0, &attrs, &text, NULL, NULL);
    /* Setting the pango attributes */
    label = gtk_label_new(text);
    gtk_label_set_attributes(GTK_LABEL(label), attrs);

    /* create a new window */
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    
    g_signal_connect (G_OBJECT (window), "destroy",
		      G_CALLBACK (gtk_main_quit), NULL);
    
    gtk_container_set_border_width (GTK_CONTAINER (window), 100);
    
    Vbox = gtk_vbox_new(FALSE,0);
    gtk_container_add (GTK_CONTAINER (window), Vbox);
        
    button = gtk_button_new_with_label("click it");
    
    g_signal_connect (G_OBJECT (button), "button_press_event",
		              G_CALLBACK (hello_print), NULL);
    
    
    gtk_box_pack_start(GTK_BOX(Vbox), button, 1, 1, 10);
    gtk_box_pack_start(GTK_BOX(Vbox), label, 1, 1, 10);
    
    gtk_widget_show (Vbox);
    gtk_widget_show (button);
    gtk_widget_show (label);
    gtk_widget_show (window);
    
    gtk_main ();

    return 0;
}
/*  ********************************************************* */
 
zentara

-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
http://mail.gnome.org/mailman/listinfo/gtk-list

[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux