Unfortunately, Application.Invoke()
is asynchronous:
private string ThreadFunction(int i)
{
string result = null;
Gtk.Application.Invoke(delegate
{
OutputStringToUserInterface("i = " + i.ToString());
result = GetStringFromUserInterface();
});
return result;
}
This means that in this example ThreadFunction()
proceeds immediately after calling Application.Invoke()
, resulting in an probably undefined state of the result
string. -- Typically ThreadFunction()
will be faster and will return with the old value (i.e. null
).
This is a workaround using ManualResetEvent
to make Application.Invoke()
synchronous:
private string ThreadFunction(int i)
{
string result = null;
ManualResetEvent ev = new ManualResetEvent(false);
Gtk.Application.Invoke(delegate
{
OutputStringToUserInterface("i = " + i.ToString());
result = GetStringFromUserInterface();
ev.Set();
});
ev.WaitOne();
return result;
}
This way, ThreadFunction()
waits until Application.Invoke()
returns, like it would do using WinForms Control.Invoke().
EDIT: Better example code
Now my question: Is there a better solution (in respect to performance/resource consumption)?