publicpartialclassForm1:Form{Thread t =null;publicForm1(){InitializeComponent();}privatevoidbutton1_Click(object sender,EventArgs e){
t =newThread(newThreadStart(Run));
t.Start();}publicvoidRun(){for(int i =1; i <20000; i++){this.Invoke(newAction(()=>{this.richTextBox1.Text ="第"+ i +"条记录同步完成";this.progressBar1.Value = i /200;this.label2.Text = i /200+"%";}));
Thread.Sleep(10);}}privatevoidbutton2_Click(object sender,EventArgs e){
Thread.Sleep(5000);}privatevoidbutton3_Click(object sender,EventArgs e){if(t.ThreadState!=ThreadState.Suspended){
t.Suspend();}}privatevoidbutton4_Click(object sender,EventArgs e){if(t.ThreadState == ThreadState.Suspended){
t.Resume();}}privatevoidbutton5_Click(object sender,EventArgs e){
t.Abort();}string str ="";privatevoidbutton6_Click(object sender,EventArgs e){
t =newThread(newParameterizedThreadStart(RunB));A a =newA(){ X =this.textBox1.Text, Y =this.textBox3.Text };
t.Start(a);//启动线程}publicvoidRun1(){
str ="["+this.textBox1.Text +"]";for(int i =1; i <20000; i++){this.Invoke(newAction(()=>{this.richTextBox1.Text = str+"第"+ i +"条记录同步完成";this.progressBar1.Value = i /200;this.label2.Text = i /200+"%";}));
Thread.Sleep(10);}}privatevoidRunB(object a){for(int i =1; i <20000; i++){// Invoke (委托 )// Action ( lambda )// Invoke ( new Action ( ()=>{} ));this.Invoke(newAction(()=>{this.richTextBox1.Text =((A)a).X +"第"+ i +"条记录同步完成"+((A)a).Y;this.progressBar1.Value = i /200;this.label2.Text = i /200+"%";}));
Thread.Sleep(10);}}privatevoidForm1_Load(object sender,EventArgs e){
Control.CheckForIllegalCrossThreadCalls =false;}classA{publicstring X {get;set;}publicstring Y {get;set;}}}
posted on
2019-11-15 12:00.net之路
阅读(201)
评论(0)
编辑收藏举报