Re: Gtk+ problem with time-consuming threads



> (FYI: I programs using C language and compile it with gcc compiler on
> MinGW.)

You can't use GTK+ from multiple threads on Windows. That is just how
it is. It is a consequence of GTK+ originally being written just for
X11.

> Could someone please give me little sample code that provide only one
> button. When that button is clicked some number of threads will do something
> time-consuming in the background (something simple like multiplying numbers
> 10000 times in the for loop, just to give me an idea).

#include <gtk/gtk.h>

static GtkWidget *label;

static gboolean
update_label (gpointer data)
{
  gchar *text = g_strdup_printf ("%"G_GINT64_FORMAT, (gint64) (gintptr) data);

  gtk_label_set_text (GTK_LABEL (label), text);

  g_free (text);

  return FALSE;
}

static gpointer
thread_func (gpointer data)
{
  static volatile int counter = 1;
  gintptr me = counter++;

  g_print ("Thread %"G_GINT64_FORMAT" starting\n", (gint64) me);

  /* Pretend doing some heavy computation here. Actually do nothing,
   * just sleep for ten seconds.
   */
  g_usleep (10*1000*1000);

  /* Set the label to this thread's number. Note that there of course
   * is no guarantee that the threads will finish in the same order as
   * started. That is not the point of this sample program.
   */
  g_idle_add (update_label, (gpointer) me);

  g_print ("Thread %"G_GINT64_FORMAT" finishing\n", (gint64) me);

  return NULL;
}

static void
doit (void)
{
  g_thread_create (thread_func, NULL, FALSE, NULL);
}

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

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

  window = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
                                           "user_data", NULL,
                                           "type", GTK_WINDOW_TOPLEVEL,
                                           "title", "Multiple threads",
                                           "border_width", 10,
                                           NULL),
                             "signal::destroy", gtk_main_quit, NULL,
                             NULL);

  hbox = g_object_new (GTK_TYPE_HBOX,
                       "GtkWidget::parent", window,
                       "GtkWidget::visible", TRUE,
                       NULL);

  label = g_object_new (GTK_TYPE_LABEL,
                        "GtkLabel::label", "0",
                        "GtkWidget::parent", hbox,
                        "GtkWidget::visible", TRUE,
                        NULL);

  button = g_object_connect (g_object_new (GTK_TYPE_BUTTON,
                                           "GtkButton::label", "Click here",
                                           "GtkWidget::parent", hbox,
                                           "GtkWidget::visible", TRUE,
                                           NULL),
                             "signal::clicked", doit, NULL,
                             NULL);

  gtk_widget_show (window);

  gtk_main ();

  return 0;
}

--tml


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