[Gtk-sharp-list] Threads safety?

Freon Freon <freongrr@gmail.com>
Mon, 23 Aug 2004 21:35:09 +0200


Hi,

I'm very new to gtk, so this question might seem trivial to some of
you, it shouldn't be too hard answering it then ;)
My application crashes when I'm adding new widgets (tabs for
instance). After some searching around I've found out it happens
because I create the widgets in a callback. In the gtk# doc it saids I
need to use Gdk.Threads.Enter() and Gdk.Threads.Enter(), but it still
doesn't work :( My application still crashes with various error
messages or visual results).

I made the simplest test possible and it still doesn't work. Shouldn't
something like this work on Windows?? I tried under Linux and it works
perfectly.

// ----------------------------------------------------------
using System;
using System.Threading;
using Gtk;

public class TestThread
{
static void Main (string[] args)
{
Window window;

GLib.Thread.Init(); // .NET needs that...
Gdk.Threads.Init ();
Gtk.Application.Init ();

window =3D new Window ("Sample");
window.Show ();

Thread t =3D new Thread(new ThreadStart(grr));
t.Start();

Gdk.Threads.Enter();
Gtk.Application.Run();
Gdk.Threads.Leave();
}

public static void grr()
{
System.Threading.Thread.Sleep(1000);
Gdk.Threads.Enter();
Window win =3D new Window("arf");
Button b =3D new Button("grr");
win.Add(b);
win.ShowAll();
Gdk.Threads.Leave();
}
}
// ----------------------------------------------------------

That code ran under Windows (with .NET or Mono) produces this output:

(<unknown>:476): Gdk-WARNING **: [Invalid UTF-8]
gdkpixmap-win32.c:196: GetDC failed: Handle de fen=DBtre non valide.

(<unknown>:476): Gdk-WARNING **: [Invalid UTF-8]
gdkpixmap-win32.c:111: DeleteObject failed: Op=DAration r=DAussie.

(<unknown>:476): Gdk-CRITICAL **: file gdkgc.c: line 90
(gdk_gc_new_with_values): assertion `drawable !=3D NULL' failed

(<unknown>:476): Gdk-CRITICAL **: file gdkgc-win32.c: line 643
(gdk_gc_set_clip_region): assertion `GDK_IS_GC (gc)' failed

(<unknown>:476): Gdk-CRITICAL **: file gdkdraw.c: line 410
(gdk_draw_rectangle): assertion `GDK_IS_DRAWABLE (drawable)' failed

(<unknown>:476): GLib-GObject-CRITICAL **: file gobject.c: line 1337
(g_object_unref): assertion `G_IS_OBJECT (object)' failed

(<unknown>:476): Gdk-CRITICAL **: file gdkdraw.c: line 410
(gdk_draw_rectangle): assertion `GDK_IS_DRAWABLE (drawable)' failed

Any idea?


Also, on the doc
(http://www.go-mono.com/docs/monodoc.ashx?tlink=3D6@ecma%3a399%23Threads%2f=
)
it says it's a good idea to call "gdk_flush()" before Threads.Leave(),
I guess it's the C gtk name, how was it called in gtk#? o_O


Thanks in advance :)