How to call winform Form Controls in multiple threads) in the previous article "How to call winform Form Controls in multiple threads", code for multi-thread calls:
Private Thread myThread;
Private void Form1_Load (object sender, EventArgs e)
{
MyThread = new Thread (new ThreadStart (RunsOnWorkerThread ));
MyThread. Start ();
}
Private void RunsOnWorkerThread ()
{
MethodInvoker mi = new MethodInvoker (SetControlsProp );
BeginInvoke (mi );
}
Private void SetControlsProp ()
{
Label1.Text = "myThread thread calls UI control ";
}
At that time, I used the way to declare the delegate. Later, I felt uncomfortable. The code was too complicated. I optimized the code below. The code in the Load event is as follows:
Private void Form1_Load (object sender, EventArgs e)
{
Thread thread = new Thread (new ThreadStart (
() =>
{
This. BeginInvoke (new EventHandler (
(Object o, System. EventArgs e1) =>{ textBox1.Text = "content set ";}));
}));
Thread. Start ();
}
After anonymous delegation is used, it is easier to understand and maintain the code.
This article is from the "Zhang Xiaoyong" blog and will not be reproduced!