Sign in to follow this  
eran65

[.net] Control.Invoke() vs. Control.BeginInvoke()

Recommended Posts

first of all,i would like to apologize for my bad grammar since English is not my native tongue.

To my understanding :

Control.Invoke(delegated_method) // Executes on the thread wich the control was created on
witch holds its handle ,typically this would be the main thread of a winform application .

Control.BeginInvoke(delegated_method // Executes asynchronously on a threadPool Thread .

according to msdn

"Executes a delegate asynchronously on the thread that the control's underlying handle was created on."

My QUESTION :

am i to understand that beginInvoke treats the main thread in this matter as it would the thread pool , and execute the delegated method on the main thread when it "gets a chance" ?

another question witch is raised ,
is it possible to create a control not on the main thread ?
if so could some one give me an example

Share this post


Link to post
Share on other sites
The big difference between Invoke and BeginInvoke is that Invoke will not return until the passed delegate has completed executing in the window thread and BeginInvoke returns immediately. So in that sense BeginInvoke is like passing it off to a worker thread. I would not recommend using it as a mechanism for doing any serious work though. If you block the application thread than all of your windows and controls will freeze, com events may not fire and things will get generally bad all around.

Don't know about running a window in another thread, it may be possible, but there is an awful lot of native stuff going on with the main thread that would need to be handled. I would not even bother with this unless you have a really good reason.

Share this post


Link to post
Share on other sites
As MSDN says, BeginInvoke will asynchronously invoke the the delegate on the thread in which the control was created. Control.Invoke is a blocking operation that will wait (in the callers thread) until the window thread the control was created under has handled the delegate you pass.

The synchronous call to Invoke can be fine, if that's what you need to do (loading a text file in the background, and then setting the contents of a text box to the text file, for instance).

However, it can also be slow if the main thread of the application is doing a lot of work (common examples include rendering in the main thread). In such a case calling BeginInvoke and then continuing on with the rest of your processing (say batch loading resources) might be more appropriate.

Share this post


Link to post
Share on other sites
I have found in more than one case using .Invoke() over .BeginInvoke() resolves problems where UI elements are not properly updated before a worker thread is complete. In one case I had a progressBar that would not hit 100% even though the integer I was using to keep track of progress the worker thread was performing was without a doubt reaching 100.

Using Invoke() and waiting for that one simple callback to the main/UI thread to update the progressBar was enough to keep the worker thread from blazing through all the work and exiting before the UI could do all of it'e updating. Once I made that change, the progressBar would perfectly and accurately reach 100% when all work was complete.

The only reason I was using multiple threads was to keep the window responsive while work was being performed- meaning the UI wasn't busy doing other real work. So, there was absolutely no noticable peformance hit by using Invoke().

I know you didn't necessarily ask this, but I thought I would let you know this practical situation and how Invoke() vs. BeginInvoke() applied to it in case it helps you in the future.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this