Windows form multi-thread UI Update (interface control)

Source: Internet
Author: User
Method 1: update a single control:

Public Delegate Void Controltextmethod (Control, String Text );
Private Void Setcontroltext (Control, String Text)
{
If ( This . Invokerequired)
{
Controltextmethod = New Controltextmethod (setcontroltext );
This . Invoke (controltextmethod, New Object [] {Control, text });
}
Else
{
Control. Text = text;
}
}

You can directly call the setcontroltext method to update the text of the control.

 

Method 2: Use uithread ":

Public Void Uithread (methodinvoker method)
{
If ( This . Invokerequired)
{
This . Invoke (method );
}
Else
{
Method. Invoke ();
}
}

Public VoidUpdateui ()
{
This. Uithread (Delegate
{
This. Label1.text ="Msg1";
This. Label2.text ="Msg2";
});
}

Call this when you want to update the interface: This. uithread (delegate {This. label1.text = "msg1 ";...});.

 

Method 3: Update multiple controls in one method:

Public Void Updateui ()
{
If ( This . Invokerequired)
{
This . Invoke ( New Methodinvoker ( Delegate {Updateui ();}));
}
Else
{
This . Label. Text = " Msg1 " ;
This . Labe2.text = " Msg2 " ;
}
}

 

 

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.