Hi
I see following code:
void UpdateMessage (string message)
{
Action action = () => txtMessage.Text = message;
this.Invoke (action);
}
Why using Action and then invoke action here? Why not just using txtMessage.Text = message to replace the code in function body?
Update
A fuller version of the code, presented in a comment, reproduced below with syntax highlighting, indentation etc.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
new Thread(Work).Start();
}
void Work()
{
Thread.Sleep(5000);
UpdateMessage("My Garden");
}
void UpdateMessage(string message) {
Action action = () => textBox1.Text = message;
this.Invoke(action);
}
}
Because this code runs on a different thread from the UI and must be marshalled across to the UI thread with
Invoke.The documentation for
Control.Invoke()states:This is all necessary because the underlying Windows framework requires that operations on a window handle are performed by the thread that owns the window handle.