C # cross thread execution error

In a C # program for modeling lan messenger, I have a callback function for beginreceive, where I need to display the text received in a specific text box. this.textBox1.Text = sb.ToString (); However, in doing so, I get the wrong firewall error. I really understand that I need to use the object.invoke method, but I could provide me with the complete code to call the delegate, because I'm still naive when it comes to threading.Thank you

+3
source share
2 answers

You need to return the work to the user interface; fortunately, this is easy:

this.Invoke((MethodInvoker) delegate {
    this.textBox1.Text = sb.ToString();
});

" " " " #, . .NET 3.5 Action, :

this.Invoke((Action) delegate {
    this.textBox1.Text = sb.ToString();
});
+8

:

void MyCallback(IAsyncResult result)
{
if (textBox1.InvokeRequired) {
    textBox1.Invoke(new Action<IAsyncResult>(MyCallBack),new object[]{result});
    return;
}
// your logic here
}
+3

Source: https://habr.com/ru/post/1705529/


All Articles