Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <gtk/gtk.h>
- #include <glib/gi18n.h>
- #include <glib.h>
- guint threadID = 0;
- guint serial_counter = 0;
- static guint
- serial_data (gpointer user_data)
- {
- // do something
- printf("counter: %d\n", serial_counter);
- serial_counter++;
- printf("data: %d\n", GPOINTER_TO_INT(user_data));
- return GPOINTER_TO_INT(user_data);
- }
- static void
- on_update_button_clicked (GtkButton* button, gpointer user_data)
- {
- guint data = GPOINTER_TO_INT(user_data) ;
- if (data == 1)
- {
- threadID = g_timeout_add(250, (GSourceFunc)serial_data, user_data);
- }
- else if (data == 0)
- {
- g_source_remove(threadID);
- threadID = 0;
- }
- }
- int
- main (int argc, char *argv[])
- {
- gtk_init (&argc, &argv);
- GtkWidget *update_button;
- GtkWidget *stop_button;
- GtkWidget *box;
- GtkWidget *window;
- window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
- gtk_window_set_title (GTK_WINDOW (window), "test.c");
- gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
- gtk_window_set_resizable (GTK_WINDOW(window), FALSE);
- box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
- update_button = gtk_button_new_with_label (_("Update"));
- stop_button = gtk_button_new_with_label (_("Stop"));
- gtk_box_pack_start (GTK_BOX (box), update_button, FALSE, FALSE, 0);
- gtk_box_pack_start (GTK_BOX (box), stop_button, FALSE, FALSE, 0);
- gtk_container_add (GTK_CONTAINER (window), box);
- g_signal_connect (update_button, "clicked", G_CALLBACK (on_update_button_clicked),(int*)1);
- g_signal_connect (stop_button, "clicked", G_CALLBACK (on_update_button_clicked),(int*) 0);
- g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
- gtk_widget_show_all (window);
- gtk_main ();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement