Tuesday, March 1, 2011

gui-thread-check

MonoDevelop often makes use of threads to run operations on the background. Although we make sure to invoke all GUI update methods through the main GUI thread, sometimes there is a bug and an update is done in the secondary thread, which causes all sort of random locks and crashes.

To make it easier to track down those bugs, I created a simple profiler module for Mono which can detect invocations to GTK# methods from a thread other than the main GUI thread. This module is available here:

https://github.com/slluis/gui-thread-check

To use it, build and install the module, and then run your application with
the command:

mono --profile=gui-thread-check yourapp.exe

If the profiler is properly installed, you'll see an output like this:

*** Running with gui-thread-check ***
*** GUI THREAD INITIALIZED: 2861676352

While the application is running, if the profiler detects a non-gui thread
invoking gtk methods, it will print a warning message together with a
stack trace. For example:

*** GTK CALL NOT IN GUI THREAD: Widget.gtk_widget_get_parent
Widget.get_Parent
SourceEditorWidget.get_TextEditor
SourceEditorWidget.get_Document
SourceEditorWidget.HandleParseInformationUpdaterWorkerThreadDoWork
BackgroundWorker.OnDoWork
BackgroundWorker.ProcessWorker

2 comments:

vip said...

Nice! Another tiny helper.

Alexander Larsson said...

As long as you take the gdk lock its not actually an error to call Gtk stuff from another thread.

For example, see:
http://blogs.operationaldynamics.com/andrew/software/gnome-desktop/gtk-thread-awareness.html

You might very well be able to have the C# binding automatically do things threadsafely instead of just warning.