Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>// Declaring a delegate function kind of lifts the function away from the // current object and the current thread, making it 'thread-safe' to call // (although it may still not be thread-safe to execute). delegate void updateTextField(string Text); private void updateText(string Text) { // This asks the control if it's running on the same thread as this method is // currently executing, i.e. can I update you directly or do I need to use // the cross thread calling method "Invoke". if (txtDelegate.InvokeRequired) { // Here we define the delegate function that's going to be Invoked. // Here it's the same function were currently within but when it's invoked // by the following line, it'll be done on the same thread as the control. // At that point, InvokeRequired will return false and the other branch // will be followed to update the actual text on the control. updateTextField del = new updateTextField(updateText); // Here we invoke the function passing in the Text we want to update the // control with as a parameter. txtDelegate.Invoke(del, new object[] { Text }); } else { // When this function is Invoked, this is the branch that will be followed // (as we're on the same thread as the control) and the text on the control // will be replaced with the text passed in from the Invoke. txtDelegate.Text = Text; } } </code></pre> <p>I hope that covers it in a bit more detail, the actual mechanics of the delegate being 'thread-safe' is beyond the scope of this answer.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload