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() is executed immediately after calling Application.Invoke() , which probably leads to the undefined state of the result string. - Usually ThreadFunction() will be faster and 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; }
Thus, ThreadFunction() waits until Application.Invoke() returns, as it would, using WinForms Control.Invoke () .
EDIT: Better Code Code
Now my question is: Is there a better solution (regarding performance / resource consumption)?
source share