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?

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 :)


Need Your Help

Random length string with random characters

c string random struct int

Hey so im struggling hard. I need to create a random string (of length 2 to 6) and generate a random character for each one 'A' to 'Z'. The problem is that I am trying to use a struct with a char*...

running ec2config.exe -sysprep remotely using powershell

powershell amazon-ec2

I am working through a process for creating an AMI from instance, for that i need to run sysprep before shutting down the machine and then create an AMI from it.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.