usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Threading;namespaceyaojiangji{ Public Partial classForm1:form {List<Label> lblist =NewList<label>(); BOOLIscreate =false; PublicForm1 () {InitializeComponent (); Control.checkforillegalcrossthreadcalls=false; } Private voidbtnStart_Click (Objectsender, EventArgs e) { if(iscreate) {iscreate=false; This. Btnstart.text ="Start"; } Else{iscreate=true; This. Btnstart.text ="End"; //method One: /*New Thread (() = { Random random = new random (); while (Iscreate) {for (int i = 0; i < 6; i++) {Lblist[i]. Text = random. Next (1, 10). ToString (); } thread.sleep (200); } } ). Start (); */ //Method Two:Thread Thread=NewThread (NewThreadStart (start)); //setting up a background threadThread. IsBackground =true; Thread. Start (); } } Private voidForm1_Load (Objectsender, EventArgs e) { for(inti =0; I <6; i++) {label label=NewLabel (); Label. Text=i.tostring (); Label. AutoSize=true; Label. Location=NewPoint ( -*i+ the, -); Lblist.add (label); This. Controls.Add (label); } } Public voidstart () {Random random=NewRandom (); while(iscreate) { for(inti =0; I <6; i++) {Lblist[i]. Text= Random. Next (1,Ten). ToString (); } thread.sleep ( $); } } }}
C # Wave machine instances (threads)