(1) 利用 Application.DoEvents()
一.进度条窗体代码
public FrmProgress(int _Minimum, int _Maximum)//带参数,表示进度条的范围的最小值和最大值
{
InitializeComponent();
progressBar1.Properties.Maximum = _Maximum;//设置范围最大值
progressBar1.Properties.Step = progressBar1.Properties.Minimum = _Minimum;//设置范围最小值
}
public void setPos(int value)//设置进度条当前进度值
{
if (value < progressBar1.Properties.Maximum)//如果值有效
{
progressBar1.Properties.Step = value + 1;//设置进度值
progressBar1.Text = (value * 100 / progressBar1.Properties.Maximum).ToString() + "%";//显示百分比
progressBar1.PerformStep();
}
Application.DoEvents();//重点,必须加上,否则父子窗体都假死
}
二.调用进度条代码
this.ActiveWrapper.Form.Enabled = false;
FrmProgress fm = new FrmProgress(0,temp.Rows.Count);
fm.Show();
fm.ngXtraLabel1.Text = "正在上传附件请稍等.......";
for (int i = 0; i < temp.Rows.Count;i++ )
{
filter = new Dictionary<string, object>();
filter.Add("id", temp.Rows[i]["id"].ToString());
filter.Add("sysno", temp.Rows[i]["sysno"].ToString());
filter.Add("byte", (byte[])temp.Rows[i]["byte"]);
proxy.SendStepData(filter);
fm.ngXtraLabel1.Text = "正在上传附件请稍等,已完成" + (i * 100 / temp.Rows.Count).ToString() + "%";
fm.setPos(i);
}
//fm.ngXtraLabel1.Text = "上传完成";
fm.Close();
this.ActiveWrapper.Form.Enabled = true;
(2) BackgroundWorker 和 progressBar1 在一个界面上
private void buttonStart_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; i <= 100; i++)
{
Thread.Sleep(30);
if (worker.CancellationPending == false)
{
worker.ReportProgress(i);
}
else
{
e.Cancel = true;
return;
}
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
int num = (int)e.ProgressPercentage;
richTextBox1.Text += num + " ";
this.progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error == null){}
else{
richTextBox1.Text += "\n操作过程中产生错误:" + e.Error;
}
}
private void buttonStop_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
buttonStop.Enabled = false;
buttonStart.Enabled = true;
}
(3) BackgroundWorker 弹出 progressBar1
主窗口:
public Form6()
{
InitializeComponent();
this.backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
this.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
}
void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{}
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 0; i < 100; i++)
{
System.Threading.Thread.Sleep(50);
worker.ReportProgress(i);
if (worker.CancellationPending) {
e.Cancel = true;
return;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
this.backgroundWorker1.RunWorkerAsync();
ProcessForm form = new ProcessForm(this.backgroundWorker1);
form.StartPosition = FormStartPosition.CenterScreen;
form.ShowDialog(this);
}
子窗口:
private BackgroundWorker backgroundWorker1;
public ProcessForm(BackgroundWorker backgroundWorker1)
{
InitializeComponent();
this.backgroundWorker1 = backgroundWorker1;
this.backgroundWorker1.ProgressChanged +=new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
this.backgroundWorker1.RunWorkerCompleted +=new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
}
void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.Close();
}
void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
}
private void button1_Click(object sender, EventArgs e)
{
this.backgroundWorker1.CancelAsync();
this.button1.Enabled = false;
this.Close();
}
(4) 利用 委托调用进度条
public delegate void RunTaskDelegate(int secends);
private void button1_Click(object sender, EventArgs e)
{
RunTaskDelegate dd = new RunTaskDelegate(RunTask);
//dd.Invoke(100);
dd.BeginInvoke(100,null,null);
}
private void RunTask(int secends)
{
for (int i = 1; i <= secends; i++)
{
Thread.Sleep(50);
SetTextMessage(i);
//this.progressBar1.Value = i;
}
}
private delegate void SetPos(int ipos);
private void SetTextMessage(int ipos)
{
if (this.InvokeRequired)
{
SetPos setpos = new SetPos(SetTextMessage);
this.BeginInvoke(setpos, new object[] { ipos });
}
else
{
this.progressBar1.Value = (int)ipos;
}
}
---------------------------------------------------------------------------------------------------
private void button1_Click(object sender, EventArgs e)
{
RunTaskDelegate runTask = new RunTaskDelegate(RunTask);
// 委托同步调用方式 (当前卡)
//runTask(Convert.ToInt16(6));
// 委托异步调用方式 (当前不卡)
runTask.BeginInvoke(Convert.ToInt16(10), null, null);
}
// 显示进度条的委托声明
delegate void ShowProgressDelegate(int totalStep, int currentStep);
// 显示进度条
void ShowProgress(int totalStep, int currentStep)
{
if (progressBarTS.InvokeRequired)
{
ShowProgressDelegate showProgress = new ShowProgressDelegate(ShowProgress);
// 为了避免工作线程被阻塞,采用异步调用委托
this.BeginInvoke(showProgress, new object[] { totalStep, currentStep });
}
else
{
progressBarTS.Maximum = totalStep;
progressBarTS.Value = currentStep;
}
}
// 执行任务的委托声明
delegate void RunTaskDelegate(int seconds);
// 执行任务
void RunTask(int seconds)
{
// 每 1 / 4 秒 显示进度一次
for (int i = 0; i < seconds * 4; i++)
{
//Thread.Sleep(250);
Thread.Sleep(100);
// 显示进度条
ShowProgress(seconds * 4, i + 1);
}
}
private void button2_Click(object sender, EventArgs e)
{
this.progressBar1.Maximum = 9;
for (int i = 0; i < 10; i++)
{
Thread.Sleep(100);
this.progressBar1.Value = i;
}
}