Click here to Skip to main content
13,055,852 members (49,373 online)
Rate this:
Please Sign up or sign in to vote.
See more:

Control.BeginInvoke(Delegate) : Executes the specified delegate "asynchronously on the thread" that the control's underlying handle was created on. The delegate is called asynchronously. You can call this method from any thread, "even the thread that owns the control's handle."

Referring the above texts in bold, what will happen actually if we call BeginInvoke method of any control in the Main Thread ( which is busy executing some other tasks)?

Will there be two threads?

Or this call to BeginInvoke will wait for Main thread to complete its executing task? if so, then UI may go into nonresponding state if the executing task of Main Thread is quite time consuming.
Posted 1-Sep-10 2:05am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

There are already two threads - the one the application is running on, and the one you started yourself. The BeginInvoke call simply does what .Net needs to have done in order to safely modify a control from a different thread.

This topic is way to extensive to answer here because this part of the site is not conducive to long threads. Please ask the same question in the C# forum so a proper discussion can be conducted.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Think of Control.BeginInvoke as a queueing mechanism. It adds the delegate to a job queue which will be executed at some point in the future by the thread that owns the control's handle.

The explanation in the link may help.[^]

piushs 3-Sep-10 7:42am
Hi Alan,

Thanks for your helpful reply.

I still have got one doubt.

Consider the following sample code.

Here Main thread starts another thread "workerThread". This worker thread calls BeginInvoke of textbox1, while Main Thread is still in DoSomeCalculation method block (assuming it is doing some very time consuming calculation). As the BeginInvoke call has to be an asynchronous call on the Main Thread and BeginInvoke does not wait, my doubt is...what will happen here?

1)Will Main Thread immediately jump to InvokeMethod(), leaving its incomplete calculation of DoSomeCalculation()....because BeginInvoke does not wait?

2)How will Main Thread keep the UI in responding state if it is busy in time consuming task of InvokeMethod().......because BeginInvoke call has to be an asynchronous call ?

Please help me to get this concept clear?

//Presuming there is a textbox on the form.
public class cSample

public delegate void InvokeDelegate();

public void InvokeMethod()
textBox1.Text = "Executed the given delegate";
//infinite loop just to assume that this is quite a time consuming task
// This method will be called when the thread is started.
public void DoWork()
textBox1.BeginInvoke(new InvokeDelegate(InvokeMethod));

public static int Main()
// Create the thread object. This does not start the thread.
Thread workerThread = new Thread(DoWork);

// Start the worker thread.
return 0;
public void DoSomeCalculation()
//some very huge calculation

Thanks in advance,


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 1 Sep 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100