I recently had one of these very bad interviews in which they play a good cop / bad cop with you. All that I answered was not enough for one of them, and my confidence declined with each passing minute. His last question, which really confused me, was this:
if InvokeRequired is required for control, will there be a difference in the execution of .Invoke or .BeginInvoke?
Let me show you an example of 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; } }
, BeginInvoke , Invoke . . , , Invoke. -, , ?
Invoke . .
Invoke
, , , , . , - , , , .
, - ( , ), Invoke. , BeginInvoke, Invoke. :)
BeginInvoke
Invoke() - , , . Invoke() , Object, .
- , , . Invoke() , . , , , , . 40 . Invoke(), . , , . , , .
(), BeginInvoke(). , Invoke(), . BeginInvoke, , , , . - , Invoke(). , , . , .
, asnyc, , .
Usually this should be faster (for a background thread) from what I am doing.