Advertisement
Mark2020H

using g_timeout_add question for voidrealms

Mar 15th, 2020
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <gtk/gtk.h>
  3. #include <glib/gi18n.h>
  4. #include <glib.h>
  5.  
  6. guint threadID = 0;
  7. guint serial_counter = 0;
  8.  
  9. static guint
  10. serial_data (gpointer user_data)
  11. {
  12.     // do something
  13.    
  14.     printf("counter: %d\n", serial_counter);
  15.     serial_counter++;
  16.     printf("data: %d\n", GPOINTER_TO_INT(user_data));
  17.     return GPOINTER_TO_INT(user_data);
  18. }
  19.  
  20. static void
  21. on_update_button_clicked (GtkButton* button, gpointer user_data)
  22. {
  23.    
  24.      guint data = GPOINTER_TO_INT(user_data) ;
  25.    
  26.     if (data == 1)
  27.     {
  28.         threadID = g_timeout_add(250, (GSourceFunc)serial_data,  user_data);
  29.     }
  30.     else if (data == 0)
  31.     {
  32.         g_source_remove(threadID);
  33.         threadID = 0;  
  34.     }
  35. }
  36.  
  37. int
  38. main (int argc, char *argv[])
  39. {
  40.    
  41.     gtk_init (&argc, &argv);
  42.     GtkWidget *update_button;
  43.     GtkWidget *stop_button;
  44.     GtkWidget *box;
  45.     GtkWidget *window;
  46.    
  47.    
  48.     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  49.     gtk_window_set_title (GTK_WINDOW (window), "test.c");
  50.     gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
  51.     gtk_window_set_resizable (GTK_WINDOW(window), FALSE);
  52.  
  53.     box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
  54.  
  55.     update_button = gtk_button_new_with_label (_("Update"));
  56.     stop_button = gtk_button_new_with_label (_("Stop"));
  57.  
  58.     gtk_box_pack_start (GTK_BOX (box), update_button, FALSE, FALSE, 0);
  59.     gtk_box_pack_start (GTK_BOX (box), stop_button, FALSE, FALSE, 0);
  60.  
  61.     gtk_container_add (GTK_CONTAINER (window), box);
  62.  
  63.     g_signal_connect (update_button, "clicked", G_CALLBACK (on_update_button_clicked),(int*)1);
  64.     g_signal_connect (stop_button, "clicked", G_CALLBACK (on_update_button_clicked),(int*) 0);
  65.     g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  66.     gtk_widget_show_all (window);
  67.  
  68.     gtk_main ();
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement