How do I invoke Windows Forms controls across threads in C #?

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Threading;//the namespace referenced by the thread operationnamespacewindowform thread Operation { Public Delegate voidRundele ();//define a delegate with no parameters     Public Partial classform1:form {thread thread; //Create a Thread object         PublicForm1 () {InitializeComponent (); //control.checkforillegalcrossthreadcalls = false; //tells the program not to check the security of the thread, it is less effort, but it will not affect the program adversely        }        BOOLIsrun =true; //Click Start        Private voidButton1_Click (Objectsender, EventArgs e) {Thread=NewThread (NewThreadStart (Run)); Thread. IsBackground=true; Thread.   Start (); //delegate starts execution           /*& button1.            Enabled = false;            Long i = 0;                while (Isrun) {ListView1.Items.Insert (0,i.tostring ());            i++; } Isrun = true;*/        }        Private voidButton2_Click (Objectsender, EventArgs e) {Isrun=false; Button1. Enabled=true ; }        Longi =0; voidRun () {//button1. Enabled = false;                     while(Isrun) {Rundele (); } Isrun=true; }        voidRundele () {if(listview1.invokerequired) {Rundele Dr=NewRundele (Rundele);  This.            Invoke (DR); }            Else{ListView1.Items.Insert (0, i.ToString ()); I++; }        }        voidExit () {ListView1.Items.Insert (0, i.ToString ()); I++; }    }}

How do I invoke Windows Forms controls across threads in C #?

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.