C# BackgroundWorker - 我应该如何摆脱 DoEvents
我试图找出处理单选按钮点击触发的后台工作人员的最佳方法。我创建了一个非常简单的表单,其中包含 3 个单选按钮和一个标签。每个单选按钮共享相同的事件 radioButton_CheckedChanged。如果事件完成,我会将标签更新为“完成”。如果您在事件完成之前单击另一个单选按钮,则将标签更新为“已取消”。下面是我在这个快速示例中编写的代码。尽管应用程序往往按预期运行,但我担心的是 Application.DoEvents 的使用。我有什么替代方案。由于显而易见的原因,我在忙时无法入睡。我的做法是错误的还是有更好的方法来做到这一点? 谢谢,波科
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = sender as RadioButton;
if (rb.Checked)
{
if (backgroundWorker1.IsBusy)
{
backgroundWorker1.CancelAsync();
while (backgroundWorker1.IsBusy)
Application.DoEvents();
}
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 0; i < 100 && !worker.CancellationPending; ++i)
Thread.Sleep(1);
if (worker.CancellationPending)
{
e.Cancel = true;
return;
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
label1.Text = "Canceled";
else
label1.Text = "Complete";
}
I'm trying to figure out the best way to handle a backgroundworker that is triggered off radio button clicks. I created a very simple form with 3 radio buttons and a label. Each of the radio buttons share the same event radioButton_CheckedChanged. If the event completes then I update the label to "Complete". If you click another radio button before the event completes then update label to Cancelled. Below is the code i have written in this quick example. Although the application tends to run as expected my concern is the use of Application.DoEvents. What are my alternatives to this. For obvious reasons i can't sleep while IsBusy. Am I going about this all wrong or is there a better way to do this?
Thanks, poco
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = sender as RadioButton;
if (rb.Checked)
{
if (backgroundWorker1.IsBusy)
{
backgroundWorker1.CancelAsync();
while (backgroundWorker1.IsBusy)
Application.DoEvents();
}
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 0; i < 100 && !worker.CancellationPending; ++i)
Thread.Sleep(1);
if (worker.CancellationPending)
{
e.Cancel = true;
return;
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
label1.Text = "Canceled";
else
label1.Text = "Complete";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将 BackgroundWorker 完成时必须运行的代码移至 RunWorkerCompleted 处理程序中。在伪代码中:
You should move the code that must run when the BackgroundWorker completes into the RunWorkerCompleted hander. In pseudo-code:
DoEvents
不应该被如此随意地对待。还有更好的方法。其中一个非常好的内容是SO中描述的。这个答案可能最适合您。因此你的解决方案变成:
DoEvents
is not supposed to be taken so casually. There are better ways. One of the very nice ones is described here in SO. This answer is probably best for you.Hence your solution becomes: