Как закрыть messagebox в backgroundworker C#, winform
Всем привет!
Я разрабатываю примерный проект о backgroundworker и progressbar control.
Ток, я встречаю проблему,
Как закрыть "MessageBox(aaaacascscascsacascsa this is tesst)" в BackgroundWorker?
Если я отменю thread backgroundworker, то messagebox все равно будет отображаться.
Это все мой код:
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.
Любые идеи, я очень благодарен!
Большое спасибо!
Что я уже пробовал:
url-ссылки:
http://stackoverflow.com/questions/10498555/calling-showdialog-in-backgroundworker
http://stackoverflow.com/questions/10283881/messagebox-on-worker-thread