初次使用 backgroundworker控件
想让backgroundworker执行一个方法,不过方法执行的时间比较长大概有几十秒想要实现:
当用户单击退出按钮的时候,检查backgroundworker控件是否正在执行
如果正在执行,则等待backgroundworker执行完毕
最后退出
技术难点:
等待backgroundworker执行完毕
目前代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace backgroundWorkDemo
{
public partial class Form1 : Form
{
private int second = 10;
private DateTime startTime;
private bool bgwOver1 = true;
private bool bgwOver2 = true; private delegate void SetTextHandler(string text); private System.Timers.Timer timer1 = new System.Timers.Timer();
private System.Timers.Timer timer2 = new System.Timers.Timer(); public Form1()
{
InitializeComponent();
} public void SetText(string text)
{
if (this.richTextBox1.InvokeRequired)
{
this.richTextBox1.Invoke(new SetTextHandler(SetText), new object[] { text });
}
else
{
this.richTextBox1.Text += text + "\r\n";
}
} private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (this.bgwOver1)
{
this.bgwOver1 = false;
this.Run("第一个线程");
}
} private void Run(string name)
{
while (startTime.AddSeconds(second) >= DateTime.Now)
{ this.SetText(DateTime.Now.ToString() + "\t" + name);
}
} private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//this.richTextBox1.Text += "操作完成";
this.SetText("第一个线程操作完成");
this.bgwOver1 = true;
} private void btnStart_Click(object sender, EventArgs e)
{
this.btnStart.Enabled = false;
this.startTime = DateTime.Now;
this.timer1.Interval = 2000;
this.timer1.Enabled = true;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed); this.timer2.Interval = 2000;
this.timer2.Enabled = true;
this.timer2.Elapsed += new System.Timers.ElapsedEventHandler(timer2_Elapsed);
} void timer2_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.backgroundWorker2.RunWorkerAsync();
} void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.backgroundWorker1.RunWorkerAsync();
} private void btnStop_Click(object sender, EventArgs e)
{
this.btnStop.Enabled = false;
this.backgroundWorker1.CancelAsync();
} private void btnDispose_Click(object sender, EventArgs e)
{
this.btnDispose.Enabled = false;
this.backgroundWorker1.Dispose();
} private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
} private void btnExit_Click(object sender, EventArgs e)
{
this.timer1.Enabled = false;
this.timer2.Enabled = false; //等待backgroundworder执行完毕
while (this.backgroundWorker1.IsBusy)
{
Application.DoEvents();
}
while (this.backgroundWorker2.IsBusy)
{
Application.DoEvents();
}
//
this.richTextBox1.Text += "\r\n停止完成";
} private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
if (this.bgwOver2)
{
this.bgwOver2 = false;
this.Run("第二个线程");
}
} private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.bgwOver2 = true;
this.SetText("第二个线程操作完成");
}
}
}解决代码: //等待backgroundworder执行完毕
while (this.backgroundWorker1.IsBusy)
{
Application.DoEvents();
}
while (this.backgroundWorker2.IsBusy)
{
Application.DoEvents();
}
//现有解决方案在停止应用程序时会出现CPU占用率很高的情况能否有种方法既能解决等待问题又能够解决CPU占用问题?
希望大家各抒己见!
想让backgroundworker执行一个方法,不过方法执行的时间比较长大概有几十秒想要实现:
当用户单击退出按钮的时候,检查backgroundworker控件是否正在执行
如果正在执行,则等待backgroundworker执行完毕
最后退出
技术难点:
等待backgroundworker执行完毕
目前代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace backgroundWorkDemo
{
public partial class Form1 : Form
{
private int second = 10;
private DateTime startTime;
private bool bgwOver1 = true;
private bool bgwOver2 = true; private delegate void SetTextHandler(string text); private System.Timers.Timer timer1 = new System.Timers.Timer();
private System.Timers.Timer timer2 = new System.Timers.Timer(); public Form1()
{
InitializeComponent();
} public void SetText(string text)
{
if (this.richTextBox1.InvokeRequired)
{
this.richTextBox1.Invoke(new SetTextHandler(SetText), new object[] { text });
}
else
{
this.richTextBox1.Text += text + "\r\n";
}
} private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (this.bgwOver1)
{
this.bgwOver1 = false;
this.Run("第一个线程");
}
} private void Run(string name)
{
while (startTime.AddSeconds(second) >= DateTime.Now)
{ this.SetText(DateTime.Now.ToString() + "\t" + name);
}
} private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//this.richTextBox1.Text += "操作完成";
this.SetText("第一个线程操作完成");
this.bgwOver1 = true;
} private void btnStart_Click(object sender, EventArgs e)
{
this.btnStart.Enabled = false;
this.startTime = DateTime.Now;
this.timer1.Interval = 2000;
this.timer1.Enabled = true;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed); this.timer2.Interval = 2000;
this.timer2.Enabled = true;
this.timer2.Elapsed += new System.Timers.ElapsedEventHandler(timer2_Elapsed);
} void timer2_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.backgroundWorker2.RunWorkerAsync();
} void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.backgroundWorker1.RunWorkerAsync();
} private void btnStop_Click(object sender, EventArgs e)
{
this.btnStop.Enabled = false;
this.backgroundWorker1.CancelAsync();
} private void btnDispose_Click(object sender, EventArgs e)
{
this.btnDispose.Enabled = false;
this.backgroundWorker1.Dispose();
} private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
} private void btnExit_Click(object sender, EventArgs e)
{
this.timer1.Enabled = false;
this.timer2.Enabled = false; //等待backgroundworder执行完毕
while (this.backgroundWorker1.IsBusy)
{
Application.DoEvents();
}
while (this.backgroundWorker2.IsBusy)
{
Application.DoEvents();
}
//
this.richTextBox1.Text += "\r\n停止完成";
} private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
if (this.bgwOver2)
{
this.bgwOver2 = false;
this.Run("第二个线程");
}
} private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.bgwOver2 = true;
this.SetText("第二个线程操作完成");
}
}
}解决代码: //等待backgroundworder执行完毕
while (this.backgroundWorker1.IsBusy)
{
Application.DoEvents();
}
while (this.backgroundWorker2.IsBusy)
{
Application.DoEvents();
}
//现有解决方案在停止应用程序时会出现CPU占用率很高的情况能否有种方法既能解决等待问题又能够解决CPU占用问题?
希望大家各抒己见!
用if就不行
奇怪啊