问题描述
大家好!
我开发了关于backgroundworker和progressbar控件的示例项目。
当前,我遇到了问题,
如何关闭MessageBox(aaaacascscascsacascsa这是测试版)在BackgroundWorker中?
如果我取消线程后台工作,那么消息框仍会显示。
这是我的全部代码:
Hi everybody!
I develop sample project about backgroundworker and progressbar control.
Current, I meet a problem,
How to closed "MessageBox(aaaacascscascsacascsa this is tesst)" in BackgroundWorker?
If i cancellation thread backgroundworker then messagebox still display.
This is all my code:
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
int _max = 1000;
private void btnStart_Click(object sender, EventArgs e)
{
//Execute th background worker doWork()
this.bgWorker.RunWorkerAsync();
this.pgbar.Maximum = this._max;
}
private void btnCancel_Click(object sender, EventArgs e)
{
//request cancellation of bgWorker
this.bgWorker.CancelAsync();
}
//Display
public void display(string text)
{
MessageBox.Show(text);
}
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
//There should be no GUI component method
for (int i = 0; i <= _max; i++)
{
//set cancellation to pending. Execute cancellation
if (this.bgWorker.CancellationPending)
{
e.Cancel = true;
}
else
{
MessageBox.Show("aaaacascscascsacascsa this is tesst");
this.simulateHeavyWork();
this.bgWorker.ReportProgress(i);
}
}
}
private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//this is updated from doWork. Its where GUI components are updated
//revices updates after 100 ms
this.pgbar.Value = e.ProgressPercentage;
this.label1.Text = e.ProgressPercentage.ToString() + "%";
}
private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//called when the heavy operation in bg in ower.Can also accept GUI component
if (e.Cancelled)
{
display("Work cancelled!");
}
else
{
display("Work completed successfully!");
}
}
//simulate complex calculations- time let refresh
private void simulateHeavyWork()
{
Thread.Sleep(100);
}
}
我使用C#,winform。
任何想法,我非常感激!
非常感谢你!
我尝试了什么:
网址参考:
http://stackoverflow.com/questions/10498555/calling-showdialog-in-backgroundworker
http://stackoverflow.com/questions/10283881/messagebox-on-worker-thread
I using C#, winform.
Any ideas, I am very grateful!
thank you very much!
What I have tried:
URL reference:
http://stackoverflow.com/questions/10498555/calling-showdialog-in-backgroundworker
http://stackoverflow.com/questions/10283881/messagebox-on-worker-thread
推荐答案
这篇关于如何在backgroundworker C#中关闭messagebox,winform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!