Click here to Skip to main content
15,922,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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);
        }
    }


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
Posted
Updated 24-Aug-16 4:03am

1 solution

MessageBox.Show blocks the current thread until the message is closed by the user. The worker thread cannot process the cancellation request whilst it is blocked.

It's also probably not a good idea to show a modal message from a worker thread. It could have unintended consequences:
MessageBoxes and worker threads[^]

What you're doing seems like a very bad idea. Do you really want your users to have to click "OK" 1000 times?
 
Share this answer
 
Comments
StackThanh 24-Aug-16 10:50am    
@Richard Deeming: Thank a lot!
About my idea, that is demo but it seemed very bad.
Your comments have helped me a lot. I will try let slove my problem.
Thank you very much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900