using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Test
{
public class MyProgress
{
//by 闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2007.10.30
private int MaxNum;
Form progressForm=null;
ProgressBar progressBar1=null;
bool Stop=false;
Label label1;
public bool ProgressStep(int step)
{
if (Stop)
{
this.Dispose();
return true;
}
if (progressBar1.Value > progressBar1.Maximum)
{
this.Dispose();
return true;
}
progressBar1.Value+= step;
label1.Text = "目前完成:" + (progressBar1.Value * 100 / progressBar1.Maximum) + "%";
Application.DoEvents();
return false;
}
private void btn_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你确定终止吗", "终止", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
Stop = false;
else
Stop = true;
}
public MyProgress(int Max, String Caption, bool IsCancel)//最大值和标题
{
progressForm = new Form();
progressForm.MinimizeBox = false;
progressForm.MaximizeBox = false;
progressForm.StartPosition = FormStartPosition.CenterScreen;
progressForm.Width = 326+19;
progressForm.Height = 96+19+20;
progressForm.Text= Caption;
progressForm.TopMost = true;//设置窗口在上边
label1 = new Label();
label1.Left = 9;
label1.Top = 15;
label1.Parent = progressForm;
progressBar1 = new ProgressBar();
progressBar1.Maximum = Max;
MaxNum = Max;
progressBar1.Left = 9;
progressBar1.Top = 25+15;
progressBar1.Width = 310;
progressBar1.Parent = progressForm;
progressBar1.Value = 0;
if (IsCancel)
{
Button btnCancel = new Button();
btnCancel.Text = "取消";
btnCancel.Left = 240;
btnCancel.Top = 54+20;
btnCancel.Parent = progressForm;
btnCancel.Click += new System.EventHandler(this.btn_Click);
}
progressForm.Show();
}
public void Dispose()
{
if (progressForm != null)
{
progressBar1.Dispose();
progressForm.Dispose();
}
}
}
}
----------------------------------
//调用测试 进度条窗口
private void button2_Click(object sender, EventArgs e)
{
MyProgress myProgress = new MyProgress(100, "进度条", true);
try
{
for (int i = 0; i < 100; i++)
{
if (myProgress.ProgressStep(1)) return;
Application.DoEvents();//让系统在百忙中抽空刷新
Thread.Sleep(100);
}
}
finally
{
myProgress.Dispose();
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Test
{
public class MyProgress
{
//by 闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2007.10.30
private int MaxNum;
Form progressForm=null;
ProgressBar progressBar1=null;
bool Stop=false;
Label label1;
public bool ProgressStep(int step)
{
if (Stop)
{
this.Dispose();
return true;
}
if (progressBar1.Value > progressBar1.Maximum)
{
this.Dispose();
return true;
}
progressBar1.Value+= step;
label1.Text = "目前完成:" + (progressBar1.Value * 100 / progressBar1.Maximum) + "%";
Application.DoEvents();
return false;
}
private void btn_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你确定终止吗", "终止", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
Stop = false;
else
Stop = true;
}
public MyProgress(int Max, String Caption, bool IsCancel)//最大值和标题
{
progressForm = new Form();
progressForm.MinimizeBox = false;
progressForm.MaximizeBox = false;
progressForm.StartPosition = FormStartPosition.CenterScreen;
progressForm.Width = 326+19;
progressForm.Height = 96+19+20;
progressForm.Text= Caption;
progressForm.TopMost = true;//设置窗口在上边
label1 = new Label();
label1.Left = 9;
label1.Top = 15;
label1.Parent = progressForm;
progressBar1 = new ProgressBar();
progressBar1.Maximum = Max;
MaxNum = Max;
progressBar1.Left = 9;
progressBar1.Top = 25+15;
progressBar1.Width = 310;
progressBar1.Parent = progressForm;
progressBar1.Value = 0;
if (IsCancel)
{
Button btnCancel = new Button();
btnCancel.Text = "取消";
btnCancel.Left = 240;
btnCancel.Top = 54+20;
btnCancel.Parent = progressForm;
btnCancel.Click += new System.EventHandler(this.btn_Click);
}
progressForm.Show();
}
public void Dispose()
{
if (progressForm != null)
{
progressBar1.Dispose();
progressForm.Dispose();
}
}
}
}
----------------------------------
//调用测试 进度条窗口
private void button2_Click(object sender, EventArgs e)
{
MyProgress myProgress = new MyProgress(100, "进度条", true);
try
{
for (int i = 0; i < 100; i++)
{
if (myProgress.ProgressStep(1)) return;
Application.DoEvents();//让系统在百忙中抽空刷新
Thread.Sleep(100);
}
}
finally
{
myProgress.Dispose();
}
}
//--------------------------------按高手的意思修改BackgroundWorker,如下
//使用BackgroundWorker异步进度条
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;//BackgroundWorker所在的
using System.Windows.Forms;
namespace Test
{
class MyNewProgress
{
private BackgroundWorker backgroundWorker = null;
//by 闫磊Email:Landgis@126.com,yanleigis@21cn.com 2007.10.31
private int MaxNum;
Form progressForm = null;
ProgressBar progressBar1 = null;
Label label1;
private void btn_Click(object sender, EventArgs e)
{
if (backgroundWorker.IsBusy)
{
if (MessageBox.Show("你确定终止吗", "终止", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
backgroundWorker.CancelAsync();
}
}
public void OnProcessCompleted(object sender, EventArgs e)
{
Dispose();
}
public void OnProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
label1.Text = "目前完成:" + (progressBar1.Value * 100 / progressBar1.Maximum) + "%";
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//return;
/*if (e.Cancelled)
{
MessageBox.Show("取消");
}
else if (e.Error != null)
{
MessageBox.Show("错误");
}
else
{
MessageBox.Show("完成");
}
*/
}
private void Form_FormClosing(object sender, FormClosingEventArgs e) //处理窗口直接关闭事件
{
if (backgroundWorker.IsBusy)
{
if (MessageBox.Show("你确定终止吗", "终止", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
backgroundWorker.CancelAsync();
else
e.Cancel = true;
}
}
//把事件传进入
private void SetbackgroundWorker(DoWorkEventHandler DoWork, ProgressChangedEventHandler worker_ProgressChanged)
{
backgroundWorker = new BackgroundWorker();
backgroundWorker.WorkerReportsProgress = true;//有进度条
backgroundWorker.WorkerSupportsCancellation = true;//是否支持异步取消
backgroundWorker.DoWork += new DoWorkEventHandler(DoWork);
backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);//做的事情
backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(OnProgressChanged);//更新进度条
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);//完成事件
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(OnProcessCompleted);
backgroundWorker.RunWorkerAsync();
}
public MyNewProgress(int Max, String Caption, bool IsCancel, DoWorkEventHandler DoWork, ProgressChangedEventHandler worker_ProgressChanged)//最大值和标题
{
progressForm = new Form();
progressForm.MinimizeBox = false;
progressForm.MaximizeBox = false;
progressForm.StartPosition = FormStartPosition.CenterScreen;
progressForm.Width = 326 + 19;
progressForm.Height = 96 + 19 + 20;
progressForm.Text = Caption;
progressForm.TopMost = true;//设置窗口在上边
label1 = new Label();
label1.Left = 9;
label1.Top = 15;
label1.Parent = progressForm;
progressBar1 = new ProgressBar();
progressBar1.Maximum = Max;
MaxNum = Max;
progressBar1.Left = 9;
progressBar1.Top = 25 + 15;
progressBar1.Width = 310;
progressBar1.Parent = progressForm;
progressBar1.Value = 0;
if (IsCancel)
{
Button btnCancel = new Button();
btnCancel.Text = "取消";
btnCancel.Left = 240;
btnCancel.Top = 54 + 20;
btnCancel.Parent = progressForm;
btnCancel.Click += new System.EventHandler(this.btn_Click);
}
progressForm.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form_FormClosing);
progressForm.Show();
SetbackgroundWorker(DoWork, worker_ProgressChanged);
}
public void Dispose()
{
if (progressForm != null)
{
progressBar1.Dispose();
progressForm.Dispose();
}
}
}
}
//测试方法,放一个按钮button和标签label1
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
DoNothing((BackgroundWorker)sender, e);
}
private void DoNothing(BackgroundWorker worker, DoWorkEventArgs e)
{
//要做的事情,写在这里
int num = 100;
for (int i = 0; i <num; i++)
{
// Check for cancellation
if (worker.CancellationPending)
{
e.Cancel = true;
break;
}
else
{
worker.ReportProgress((i + 1) * (100 / num));
Thread.Sleep(100);
}
}
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = e.ProgressPercentage.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
MyNewProgress myProgress = new MyNewProgress(100, "进度条", true, worker_DoWork, worker_ProgressChanged);
}
//使用BackgroundWorker异步进度条
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;//BackgroundWorker所在的
using System.Windows.Forms;
namespace Test
{
class MyNewProgress
{
private BackgroundWorker backgroundWorker = null;
//by 闫磊Email:Landgis@126.com,yanleigis@21cn.com 2007.10.31
private int MaxNum;
Form progressForm = null;
ProgressBar progressBar1 = null;
Label label1;
private void btn_Click(object sender, EventArgs e)
{
if (backgroundWorker.IsBusy)
{
if (MessageBox.Show("你确定终止吗", "终止", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
backgroundWorker.CancelAsync();
}
}
public void OnProcessCompleted(object sender, EventArgs e)
{
Dispose();
}
public void OnProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
label1.Text = "目前完成:" + (progressBar1.Value * 100 / progressBar1.Maximum) + "%";
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//return;
/*if (e.Cancelled)
{
MessageBox.Show("取消");
}
else if (e.Error != null)
{
MessageBox.Show("错误");
}
else
{
MessageBox.Show("完成");
}
*/
}
private void Form_FormClosing(object sender, FormClosingEventArgs e) //处理窗口直接关闭事件
{
if (backgroundWorker.IsBusy)
{
if (MessageBox.Show("你确定终止吗", "终止", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
backgroundWorker.CancelAsync();
else
e.Cancel = true;
}
}
//把事件传进入
private void SetbackgroundWorker(DoWorkEventHandler DoWork, ProgressChangedEventHandler worker_ProgressChanged)
{
backgroundWorker = new BackgroundWorker();
backgroundWorker.WorkerReportsProgress = true;//有进度条
backgroundWorker.WorkerSupportsCancellation = true;//是否支持异步取消
backgroundWorker.DoWork += new DoWorkEventHandler(DoWork);
backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);//做的事情
backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(OnProgressChanged);//更新进度条
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);//完成事件
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(OnProcessCompleted);
backgroundWorker.RunWorkerAsync();
}
public MyNewProgress(int Max, String Caption, bool IsCancel, DoWorkEventHandler DoWork, ProgressChangedEventHandler worker_ProgressChanged)//最大值和标题
{
progressForm = new Form();
progressForm.MinimizeBox = false;
progressForm.MaximizeBox = false;
progressForm.StartPosition = FormStartPosition.CenterScreen;
progressForm.Width = 326 + 19;
progressForm.Height = 96 + 19 + 20;
progressForm.Text = Caption;
progressForm.TopMost = true;//设置窗口在上边
label1 = new Label();
label1.Left = 9;
label1.Top = 15;
label1.Parent = progressForm;
progressBar1 = new ProgressBar();
progressBar1.Maximum = Max;
MaxNum = Max;
progressBar1.Left = 9;
progressBar1.Top = 25 + 15;
progressBar1.Width = 310;
progressBar1.Parent = progressForm;
progressBar1.Value = 0;
if (IsCancel)
{
Button btnCancel = new Button();
btnCancel.Text = "取消";
btnCancel.Left = 240;
btnCancel.Top = 54 + 20;
btnCancel.Parent = progressForm;
btnCancel.Click += new System.EventHandler(this.btn_Click);
}
progressForm.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form_FormClosing);
progressForm.Show();
SetbackgroundWorker(DoWork, worker_ProgressChanged);
}
public void Dispose()
{
if (progressForm != null)
{
progressBar1.Dispose();
progressForm.Dispose();
}
}
}
}
//测试方法,放一个按钮button和标签label1
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
DoNothing((BackgroundWorker)sender, e);
}
private void DoNothing(BackgroundWorker worker, DoWorkEventArgs e)
{
//要做的事情,写在这里
int num = 100;
for (int i = 0; i <num; i++)
{
// Check for cancellation
if (worker.CancellationPending)
{
e.Cancel = true;
break;
}
else
{
worker.ReportProgress((i + 1) * (100 / num));
Thread.Sleep(100);
}
}
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = e.ProgressPercentage.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
MyNewProgress myProgress = new MyNewProgress(100, "进度条", true, worker_DoWork, worker_ProgressChanged);
}