Is there a C# / Windows Forms equivalent to Android's runOnUiThread?
For example, say I am running a (long-running) task on a worker thread and at the end I want to post the result to a GUI control (which of course I can't do from the worker thread). Is there a function as simple as runOnUiThread that would allow me to post that refresh-display for processing by the UI thread?
Each control in WinForms inherits the Invoke
and BeginInvoke
methods.
Invoke will run the delegate synchronously whereas BeginInvoke runs it asynchronously.
A typical way to do this is with a System.Windows.Forms.Control:
control.BeginInvoke((MethodInvoker)delegate { ... });
But, the control's handle must have already been initialized on the UI thread. A simple
IntPtr ignored = control.Handle;
on the UI thread will accomplish that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With