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 #?