I have a dialog based application in which I will delegating the I/O operation read write to different thread.
I just want to clear is there any difference between two approaches..
First approach: ( I am doing this in ,my main form -Form.cs)
delegate void Action(); Action _action = new Action(Method); this.BeginInvoke(_action);
Second approach:
Thread th = new Thread( new ThreadStart(_action)); th.Start();
I noticed that BeginInvoke hangs the UI for a second while second approach don't ..
Please help
Invoke: Executes on the UI thread, but calling thread waits for completion before continuing. Control. BeginInvoke: Executes on the UI thread, and calling thread doesn't wait for completion.
BeginInvoke(Action) Executes the specified delegate asynchronously on the thread that the control's underlying handle was created on. BeginInvoke(Delegate) Executes the specified delegate asynchronously on the thread that the control's underlying handle was created on.
Invoke : Executes synchronously, on the same thread.
BeginInvoke
will post the action in the message queue of the message pump on the same thread as the Form
, it will not create a new thread.
Control.BeginInvoke
behaves similar to an asynchronous thread start, but has important internal differences.
Read in further detail an article here.
BeginInvokes executes the delegate asynchronously on the UI thread (which is why it hangs the UI), by posting a message to the window. That's what you need to do if the code in the delegate accesses the UI.
The approach with Thread.Start executes the delegates on a new, independant thread.
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