Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a C# / Windows Forms equivalent to Android's runOnUiThread?

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?

like image 396
scatmoi Avatar asked Oct 19 '25 20:10

scatmoi


2 Answers

Each control in WinForms inherits the Invoke and BeginInvoke methods.

Invoke will run the delegate synchronously whereas BeginInvoke runs it asynchronously.

like image 51
heavyd Avatar answered Oct 22 '25 10:10

heavyd


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.

like image 29
JMH Avatar answered Oct 22 '25 11:10

JMH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!