Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Threading
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 1-Sep-10 3:05am
piushs131
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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.
http://www.drdobbs.com/184429353[^]
 
Alan.
  Permalink  
Comments
piushs at 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)

  Print Answers RSS
0 OriginalGriff 200
1 PIEBALDconsult 150
2 BillWoodruff 148
3 Jochen Arndt 135
4 DamithSL 130
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 1 Sep 2010
Copyright © CodeProject, 1999-2014
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