Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between BeginInvoke and Thread.Start

Tags:

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

like image 205
Ashish Ashu Avatar asked Aug 04 '09 09:08

Ashish Ashu


People also ask

What is the difference between Invoke and BeginInvoke?

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.

What is BeginInvoke?

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.

Is invoke synchronous?

Invoke : Executes synchronously, on the same thread.


2 Answers

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.

like image 135
Kenan E. K. Avatar answered Sep 25 '22 05:09

Kenan E. K.


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.

like image 37
Thomas Levesque Avatar answered Sep 26 '22 05:09

Thomas Levesque