Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

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

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.
 
Share this answer
 
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.
http://www.drdobbs.com/184429353[^]

Alan.
 
Share this answer
 
Comments
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";
while(true)
{
//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.
workerThread.Start();
DoSomeCalculation();
return 0;
}
public void DoSomeCalculation()
{
//some very huge calculation
}

}
Thanks in advance,

Piush

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900