using System;
using System. Collections. Generic;
using System. ComponentModel;
using System. Data;
using System. Drawing;
using System. Linq;
using System. Text;
using System. Windows. Forms;
namespace Bwg_Test
{
public partial class Form1 : Form
{
public Form1 ( )
{
InitializeComponent ( ) ;
}
private void backgroundWorker1_ProgressChanged ( object sender, ProgressChangedEventArgs e)
{
this . progressBar1. Value = e. ProgressPercentage;
this . label1. Text = e. UserState. ToString ( ) ;
this . label1. Update ( ) ;
}
private void backgroundWorker1_RunWorkerCompleted ( object sender, RunWorkerCompletedEventArgs e)
{
MessageBox. Show ( "运算终于完成了" ) ;
}
private void backgroundWorker1_DoWork ( object sender, DoWorkEventArgs e)
{
work ( this . backgroundWorker1) ;
}
private bool work ( BackgroundWorker bk)
{
int tatle = 100 ;
for ( int i = 0 ; i < tatle; i++ )
{
if ( bk. CancellationPending)
{
bk. ReportProgress ( i, String. Format ( "当前值是 {0},操作被用户申请中断" , i) ) ;
return false ;
}
bk. ReportProgress ( i, String. Format ( "当前值是 {0} " , i) ) ;
}
return true ;
}
private void button2_Click ( object sender, EventArgs e)
{
this . backgroundWorker1. CancelAsync ( ) ;
}
private void button1_Click ( object sender, EventArgs e)
{
this . backgroundWorker1. RunWorkerAsync ( ) ;
}
private void button3_Click ( object sender, EventArgs e)
{
this . Close ( ) ;
}
}
}
namespace Bwg_Test
{
partial class Form1
{
private System. ComponentModel. IContainer components = null ;
protected override void Dispose ( bool disposing)
{
if ( disposing && ( components != null ) )
{
components. Dispose ( ) ;
}
base . Dispose ( disposing) ;
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent ( )
{
this . backgroundWorker1 = new System. ComponentModel. BackgroundWorker ( ) ;
this . progressBar1 = new System. Windows. Forms. ProgressBar ( ) ;
this . label1 = new System. Windows. Forms. Label ( ) ;
this . button1 = new System. Windows. Forms. Button ( ) ;
this . button2 = new System. Windows. Forms. Button ( ) ;
this . button3 = new System. Windows. Forms. Button ( ) ;
this . SuspendLayout ( ) ;
this . backgroundWorker1. WorkerReportsProgress = true ;
this . backgroundWorker1. DoWork + = new System. ComponentModel. DoWorkEventHandler ( this . backgroundWorker1_DoWork) ;
this . backgroundWorker1. ProgressChanged + = new System. ComponentModel. ProgressChangedEventHandler ( this . backgroundWorker1_ProgressChanged) ;
this . backgroundWorker1. RunWorkerCompleted + = new System. ComponentModel. RunWorkerCompletedEventHandler ( this . backgroundWorker1_RunWorkerCompleted) ;
this . progressBar1. Location = new System. Drawing. Point ( 225 , 288 ) ;
this . progressBar1. Name = "progressBar1" ;
this . progressBar1. Size = new System. Drawing. Size ( 317 , 24 ) ;
this . progressBar1. TabIndex = 0 ;
this . label1. AutoSize = true ;
this . label1. Location = new System. Drawing. Point ( 334 , 164 ) ;
this . label1. Name = "label1" ;
this . label1. Size = new System. Drawing. Size ( 62 , 18 ) ;
this . label1. TabIndex = 1 ;
this . label1. Text = "label1" ;
this . button1. Location = new System. Drawing. Point ( 109 , 366 ) ;
this . button1. Name = "button1" ;
this . button1. Size = new System. Drawing. Size ( 106 , 36 ) ;
this . button1. TabIndex = 2 ;
this . button1. Text = "button1" ;
this . button1. UseVisualStyleBackColor = true ;
this . button1. Click + = new System. EventHandler ( this . button1_Click) ;
this . button2. Location = new System. Drawing. Point ( 290 , 366 ) ;
this . button2. Name = "button2" ;
this . button2. Size = new System. Drawing. Size ( 106 , 36 ) ;
this . button2. TabIndex = 3 ;
this . button2. Text = "button2" ;
this . button2. UseVisualStyleBackColor = true ;
this . button2. Click + = new System. EventHandler ( this . button2_Click) ;
this . button3. Location = new System. Drawing. Point ( 491 , 366 ) ;
this . button3. Name = "button3" ;
this . button3. Size = new System. Drawing. Size ( 106 , 36 ) ;
this . button3. TabIndex = 4 ;
this . button3. Text = "button3" ;
this . button3. UseVisualStyleBackColor = true ;
this . button3. Click + = new System. EventHandler ( this . button3_Click) ;
this . AutoScaleDimensions = new System. Drawing. SizeF ( 9F , 18F ) ;
this . AutoScaleMode = System. Windows. Forms. AutoScaleMode. Font;
this . ClientSize = new System. Drawing. Size ( 800 , 450 ) ;
this . Controls. Add ( this . button3) ;
this . Controls. Add ( this . button2) ;
this . Controls. Add ( this . button1) ;
this . Controls. Add ( this . label1) ;
this . Controls. Add ( this . progressBar1) ;
this . Name = "Form1" ;
this . Text = "Form1" ;
this . ResumeLayout ( false ) ;
this . PerformLayout ( ) ;
}
#endregion
private System. ComponentModel. BackgroundWorker backgroundWorker1;
private System. Windows. Forms. ProgressBar progressBar1;
private System. Windows. Forms. Label label1;
private System. Windows. Forms. Button button1;
private System. Windows. Forms. Button button2;
private System. Windows. Forms. Button button3;
}
}