黑马程序员技术交流社区
标题:
利用BackgroundWorker实现的进度条
[打印本页]
作者:
aisini
时间:
2014-8-20 14:39
标题:
利用BackgroundWorker实现的进度条
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
namespace AskAnswers
{
public class ShowProgressStatus : Form
{
[STAThread]
static void Main()
{
Application.Run(new ShowProgressStatus());
}
public ShowProgressStatus()
{
InitializeComponent();
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with an editor
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.label1 = new System.Windows.Forms.Label();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.btnProcess = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
label1.Location = new System.Drawing.Point(32, 40);
label1.Text = "Progress Value";
label1.Size = new System.Drawing.Size(88, 24);
label1.TabIndex = 2;
progressBar1.Maximum = 10;
progressBar1.Location = new System.Drawing.Point(8, 312);
progressBar1.Minimum = 0;
progressBar1.TabIndex = 0;
progressBar1.Value = 0;
//We have calculated the excat size which will result in only 20 boxes to be drawn
progressBar1.Size = new System.Drawing.Size(520, 40);
progressBar1.Step = 1;
btnProcess.Location = new System.Drawing.Point(152, 168);
btnProcess.Size = new System.Drawing.Size(144, 48);
btnProcess.TabIndex = 1;
btnProcess.Text = "Process";
btnProcess.Click += new System.EventHandler(btnProcess_Click);
textBox1.Location = new System.Drawing.Point(136, 40);
textBox1.Text = "0";
textBox1.TabIndex = 3;
textBox1.Size = new System.Drawing.Size(184, 20);
this.Text = "Display Progress Status";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(616, 393);
this.StartPosition = FormStartPosition.CenterScreen;
this.Controls.Add(textBox1);
this.Controls.Add(label1);
this.Controls.Add(btnProcess);
this.Controls.Add(progressBar1);
// To report progress from the background worker we need to set this property
backgroundWorker1.WorkerReportsProgress = true;
// This event will be raised on the worker thread when the worker starts
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
// This event will be raised when we call ReportProgress
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
//当backgroundWorker做完后发生的事件
}
// On worker thread so do our thing!
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
isProcessRunning = true;
Thread.Sleep(100);
// Your background task goes here
for (int i = 0; i <= fileCount; i++)
{
// Report progress to 'UI' thread
backgroundWorker1.ReportProgress(i);
// Simulate long task
System.Threading.Thread.Sleep(200);
textBox1.BeginInvoke(
new Action(() =>
{
textBox1.Text = (i + 1).ToString();
}
));
}
}
// Back on the 'UI' thread so we can update the progress bar
void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// The progress percentage is a property of e
progressBar1.Value = e.ProgressPercentage;
}
void btnProcess_Click(object sender, EventArgs e)
{
if (isProcessRunning)
{
MessageBox.Show("A process is already running.");
return;
}
string[] files = Directory.GetFiles(@"c:\", "*");
progressBar1.Maximum = files.Length;
fileCount = files.Length;
// Start the background worker
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//如果数组中有东西,那么加入ComboBox
progressBar1.Value = progressBar1.Minimum;
isProcessRunning = false;
}
private System.Windows.Forms.Button btnProcess;
private System.ComponentModel.Container components;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ProgressBar progressBar1;
private System.ComponentModel.BackgroundWorker backgroundWorker1;
private bool isProcessRunning;
private int fileCount;
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2