通过BackgroundWorker实现进度条的显示,并将进度显示在进度条上
BackgroundWorker的类允许您在一个单独的专用线程运行的操作。喜欢下载和数据库事务耗时的操作可能会导致用户界面(UI),以看起来就像是已经停止,而他们正在运行响应。当你想要一个负责任的UI,你都面临着这样的业务相关的长期拖延,BackgroundWorker的类提供了一个方便的解决方案。
其中,对于将进度条显示在Progressbar上,是通过progressBar1.CreateGraphics()进行重绘。
代码如下:
//show the percentage
using (Graphics gr = progressBar1.CreateGraphics())
{
gr.DrawString(e.ProgressPercentage.ToString() + "%", SystemFonts.DefaultFont,
Brushes.Black,
new PointF(progressBar1.Width / 2 - (gr.MeasureString(e.ProgressPercentage.ToString() + "%",
SystemFonts.DefaultFont).Width / 2.0F),
progressBar1.Height / 2 - (gr.MeasureString(e.ProgressPercentage.ToString() + "%",
SystemFonts.DefaultFont).Height / 2.0F)));
}
完整代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BackgroundWorker
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
System.ComponentModel.BackgroundWorker worker = sender as System.ComponentModel.BackgroundWorker;
for (int i = 0; i <= 100; i++)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
System.Threading.Thread.Sleep(500);
worker.ReportProgress(i);
}
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
resultLabel.Text=(e.ProgressPercentage.ToString()+"%");
progressBar1.Value =Convert.ToInt32( e.ProgressPercentage.ToString());
//show the percentage
using (Graphics gr = progressBar1.CreateGraphics())
{
gr.DrawString(e.ProgressPercentage.ToString() + "%", SystemFonts.DefaultFont,
Brushes.Black,
new PointF(progressBar1.Width / 2 - (gr.MeasureString(e.ProgressPercentage.ToString() + "%",
SystemFonts.DefaultFont).Width / 2.0F),
progressBar1.Height / 2 - (gr.MeasureString(e.ProgressPercentage.ToString() + "%",
SystemFonts.DefaultFont).Height / 2.0F)));
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled == true)
{
resultLabel.Text = "Canceled!";
}
else if (e.Error != null)
{
resultLabel.Text = "Error: " + e.Error.ToString();
}
else
{
resultLabel.Text = "Done!";
}
}
private void startAsyncButton_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy != true)
{
backgroundWorker1.RunWorkerAsync();
}
}
private void cancelAsyncButton_Click(object sender, EventArgs e)
{
if (backgroundWorker1.WorkerSupportsCancellation == true)
{
backgroundWorker1.CancelAsync();
}
}
}
}
备注:不要在你的DoWork的事件处理程序中操纵任何用户界面对象。相反,通过ProgressChanged和RunWorkerCompleted事件的来操作用户界面。
BackgroundWorker的事件不会跨越AppDomain编组。不要使用BackgroundWorker组件在多个AppDomain中进行多线程操作。
其中,对于Progressbar的重绘,可以参照如下文章
Add the Percent (or Any Text) into a Standard Progress Bar Control
http://www.codeproject.com/Articles/31406/Add-the-Percent-or-Any-Text-into-a-Standard-Progre
对于BackgroundWorker,可以参照如下文章
BackgroundWorker Class
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx