C # handles a form control within a thread using a system delegate

Source: Internet
Author: User
Tags clear screen

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.