[Gtk-sharp-list] waiting for an asynchronous operation to finish

Jeroen Zwartepoorte jeroen.zwartepoorte at gmail.com
Wed Sep 28 09:22:54 EDT 2005


So i've got a Gtk# program (VfsNodeView) that starts an asynchronous
operation and then needs to wait for it to finish. How to do this?

The operation in question is Gnome.Vfs.Drive.Mount(). This starts a
new GThread (in native C code) to perform the mount. I pass a callback
handler that gets called when the operation has finished. I want my
main thread/loop to wait until it has finished.

In C (gtkfilesystemgnomevfs.c in libgnomeui) this is accomplished by
starting a new MainLoop in the main thread (MainLoop.Run()) and then
quitting that mainloop from the callback handler. However, that
approach either doesn't work in Gtk# or exposes a serious bug: i
randomly get this crash: "*** glibc detected *** mono: double free or
corruption" etc.

System.Threading.Thread has Suspend() and Resume() methods, but how do
these interact with the GLib mainloop?

So what i want to do is this (pseudo code follows):

private bool succeeded;

...
Drive.Mount (new CallbackHandler (OnMount));
Suspend ();
if (!succeeded)
  DisplayErrorDialog();
else
  DoStuff ();
...

// Gets called from a separate thread.
private void OnMount (bool succeeded)
{
  this.succeeded = succeeded;
  Resume ();
}

Jeroen


More information about the Gtk-sharp-list mailing list