The System.Windows.Forms.MethodInvoker type is a system-defined delegate that is used to invoke methods with no parameters.
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 invokes UI control";
}
Two, direct use System.eventhandle (can with parameter)
Private Thread mythread;
private void Form1_Load (object sender, EventArgs e)
{
Mythread = new Thread (new ThreadStart (Runsonworkerthread));
Mythread.start ();
}
private void Runsonworkerthread ()
{
Dosomethingslow ();
String plist = "Mythread thread invokes UI control";
Label1. BeginInvoke (New System.EventHandler (UpdateUI), plist);
}
Directly with System.EventHandler, there is no need to customize the delegate
private void UpdateUI (object o, System.EventArgs e)
{
UI thread Setting Label1 property
Label1. Text = o.tostring () + "Success!";
}
Third, the demonstration procedure
<summary>
Provides system delegate event calls that resolve the purpose of manipulating interface controls within a thread
</summary>
<param name= "obj" ></param>
<param name= "E" ></param>
private void Updatelistbox (Object Obj,system.eventargs e)
{
Force type conversions
list<string> paramlist = (list<string>) obj;
if (paramlist[0] = = "0")
{
This.lstBoxCatchData.Items.Clear ();
}
else if (paramlist[0] = = "1")
{
THIS.LSTBOXCATCHDATA.ITEMS.ADD (Paramlist[1]. ToString ());
}
}
Call part in thread:
Initialize parameter list
list<string> paramlist = new list<string> ();
Paramlist.add ("0");
Paramlist.add ("");
In this way, the system is commissioned to implement the operating system interface controls within the thread.
Paramlist[0] = "0"; PARAMLIST[1] = "clear screen information";
Lstboxcatchdata.begininvoke (New System.EventHandler (Updatelistbox), paramlist);
End.