Re: Dialog boxes in seperate thread



2008/4/30 David Conley (FRD) <dconley frd co uk>:
Are modal dialog boxes possible in threads? I have this call in a thread
 seperate to the main gtk thread:

 gtk_dialog_run(GTK_DIALOG(errorDialog));

 and obviously that locks that thread while the main thread continues but
 nothing updates the display in the blocked thread and the app becomes
 unresponsive.

GTK is happiest if you keep all gtk_* calls in the main thread, and if
the main thread does (almost) nothing but run the gtk event loop.

If you need to start a thread to do a long running task, don't call
gtk stuff from the thread, instead send a message to the main thread
and ask it to make the calls on the thread's behalf. This is easy to
do with g_idle_add(), which you can call safely from a worker thread.

Something like:

on_button_click( ..
{
  if (!app->bg_job)
    app->bg_job = g_thread_create( background_job ..
}

background_job ( ..
{
  for (int i = 0; i < 100; i++) {
    char *str = g_strdup_printf ("... still working! %d", i);
    g_idle_add (process_message, str);
    sleep (1)
  }
}

process_message (.. char *str)
{
  gtk_label_set (app->status, str);
  g_free (str);
}

John



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