C # (C # Training) Why does multi- threading control WinForm require a delegate? for example, I have a new thread A, in the a thread to the WinForm list control display data, I need to use a delegate function to implement.
Because WinForm is created by the master thread , you may have two threads accessing the same resource at the same time with another thread, which is prone to errors, such as a thread changing the color of the window to red, the B thread taking the window color, If these two threads happen to meet together, maybe a slightly before B, then is a b taken out of the red, if slightly later than the original color, and this and CPU busy, time slice of the rotation is related, is a random situation, then b out of the color is not reliable, so in order to avoid this situation using the delegation, The b thread sends a delegate to the A thread, which completes the color-fetching work by a thread, which guarantees the stability of the color-fetching work.
If you really want to access WinForm through another thread , you can add the window's constructor
Control.checkforillegalcrossthreadcalls = false;
This can block the error, but the recommendation is to use the delegate
This requires the use of a delegate.
You declare a delegate, and then you give the function of an interface to him.
You can just call this delegate on your thread.
C # try to write a multi- threaded problem (delegate, Invoke (), BeginInvoke ())
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using System.Threading;
Using System.Diagnostics;
namespace _
{Public partial class Form1:form
{publicdelegatevoid del (string str);
Publicdelegatevoid Del1 (string str1);
Thread thread1,thread2;
Public Form1 ()
{InitializeComponent ();
} privatevoid button1_click (object sender, EventArgs e)
{this.thread1 = new Thread (new ThreadStart (THIS.SA));
This.thread1.Start ();
MessageBox.Show (this,);
this.button1.Enabled = false;
Gets the current thread good
int thread1id = Thread.CurrentThread.GetHashCode ();
String strthreadid = Convert.ToString (thread1id);
MessageBox.Show (This,strthreadid);
} privatevoid Progressset (string text)
{progressbar1.value = 0;
Progressbar1.maximum = 999999;
for (int i = 0; i < 999999; i++)
{progressbar1.value++;
} progressbar1.value = 0;
} privatevoid SA ()
{del Pro = new del (progressset);
This. Invoke (Pro, newobject[] {"Hello"});
} privatevoid button2_click (object sender, EventArgs e)
{this.thread2 = new Thread (THIS.SA1);
Thread2. Start ();
this.button2.Enabled = false;
Gets the current thread good
int thread2id = Thread.CurrentThread.GetHashCode ();
String strthreadid = Convert.ToString (thread2id);
MessageBox.Show (this, strthreadid);
} privatevoid SA1 ()
{//Other actions
For example, set the TextBox content of the interface
Del1 mi1 = new Del1 (Updatetextbox);
This. Invoke (MI1, newobject[] {"I am a text box"});
}//methods to update the interface
Privatevoid Updatetextbox (String str)
{//update
This.textBox1.Text = str;
} } }
C # Why multithreading control WinForm need to use a delegate?