Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interview Question: When Control.InvokeRequired do you use Control.Invoke or Control.BeginInvoke?

I have had recently one of those really bad interviews, where they play good cop/bad cop with you. Whatever I replied wasn't good enough for one of them and my confidence was shrinking minute by minute. His final question that really confused me was the following:

if a control would need InvokeRequired would there be a difference in doing .Invoke or .BeginInvoke?

Let me show you an example, how I understand it:

public delegate string WorkLongDelegate(int i);

var del = new WorkLongDelegate(WorkLong);
var callback = new AsyncCallback(CallBack);
del.BeginInvoke(3000, callback, del);

public string WorkLong(int i)
{
      Thread.Sleep(i);
      return (string.Format("Work was done within {0} seconds.", i));            
}

private void CallBack(IAsyncResult ar)
{
    var del = (WorkLongDelegate) ar.AsyncState;
    SetText2(del.EndInvoke(ar));
}

private void SetText2(string s)
{
   if(InvokeRequired)
   {
       // What is the difference between BeginInvoke and Invoke in below?
       BeginInvoke(new MethodInvoker(() => textBox1.Text = s)); 
   }
   else
   {
       textBox1.Text = s;
   }
}

I mentioned that BeginInvoke would do it asynchronously while Invoke would be halting the UI thread until its executed. But that wasn't good enough. Nonetheless, I don't understand the performance implication in here if I used an Invoke instead. May someone please enlighten me?

like image 990
Houman Avatar asked Oct 24 '10 17:10

Houman


People also ask

What is the difference between Invoke and BeginInvoke method in C#?

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 Invoke required?

InvokeRequired is checking to see if it's on the UI thread if not it equals True , Me. Invoke is asking for a delegate to handle communication between the diff threads. As for your side note. I typically use an event to pass data - this event is still on the diff thread, but like above you can delegate the work.

What is use of InvokeRequired in c#?

If the control's handle does not yet exist, InvokeRequired searches up the control's parent chain until it finds a control or form that does have a window handle. If no appropriate handle can be found, the InvokeRequired method returns false .


1 Answers

Invoke doesn't halt the UI thread. It blocks the calling thread from continuing until the UI thread has completed.

So really, the question is whether you want the background operation to continue before the UI has finished updating. Usually I believe this is the case - for example, if you're just providing a progress report to the UI, you don't want to stop working just because the UI thread hasn't caught up yet.

On the other hand, if you need to fetch something from the UI thread (which is pretty rare, admittedly) then you might want to use Invoke instead. I'd say you should use BeginInvoke unless you've got a specific reason to use Invoke. But either way, you should understand the difference :)

like image 82
Jon Skeet Avatar answered Sep 28 '22 09:09

Jon Skeet