Problems with Threading Concept



Hi all,

I have GUI developed based on gtk+-2.2.0,

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...

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

 int flag=1,toggle=0;
    int i=0;
    pthread_t yes_tid; 

void hello()
{

    while (flag)
    {
  gdk_threads_enter ();

      printf ("%d %d\n",i++,flag);
      if (gtk_events_pending())
      gtk_main_iteration(); // Handle unprocessed GTK events

  gdk_threads_leave ();
    }

}

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;
    g_thread_init (NULL);
    gdk_threads_init ();

  gdk_threads_enter ();

    gtk_init (&argc, &argv);
    
    /* 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);
    
    button = gtk_button_new_with_label("click it");
    
    g_signal_connect (G_OBJECT (button), "clicked",
		      G_CALLBACK (hello_print), NULL);
    
    gtk_container_add (GTK_CONTAINER (window), button);
    
    gtk_widget_show (button);
    gtk_widget_show (window);
    
    gtk_main ();

   gdk_threads_leave ();

    return 0;
}

//////////////////////////////////////////////////////
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...

Regards,
Naveen.




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