Click here to Skip to main content
15,881,455 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
Hello,


In my mainForm I have a BackgroundWorker. I call a WaitDialog when the task for bgworker is started and close it when completed. I want to have a Cancel button in WaitDialog and when clicked, notify the bgworker task to cancel the task. This is how I call and show my waitDlg in mainForm :

C#
currentState = this.CONNECTING;
backgroundWorker1.RunWorkerAsync();
msg = "Connecting...";
waitDlg.set(msg, "");
if (!waitDlg.IsAccessible)
    waitDlg.ShowDialog();


In my waitDlg, when the user clicks Cancel button, at present I jsut set a flag Cancel = true.

I tried using EventHadler n cancelbutton and that handles in mainForm, but the mainForm's method is never called.

How to achieve this goal ? I have spent lots of time on this but couldn't find the solution. Can anyone help me with this. I am kind of in hurry now - as have spend many hrs solving this problem.

Any help is highly appreciated.

Thanks
Posted

1 solution

You should create a public property on the main form which returns the background worker. Then in your progress form you should pass a reference to the main form in the constructor. In the cancel button event call the bgw's CancelAsync event, something like this

C#
BackgroundWorker bgw;

        public BackgroundWorker formWorker
        {
            get
            {
                return bgw;
            }
        }

        public frmParent()
        {
            InitializeComponent();
            bgw = new BackgroundWorker();
            bgw.WorkerSupportsCancellation = true;
            bgw.DoWork +=new DoWorkEventHandler(backgroundWorker_DoWork);
            bgw.RunWorkerAsync();
        }

        private void bt_add_Click(object sender, EventArgs e)
        {
            frmProgress progress = new frmProgress(this);
            progress.ShowDialog();
        }

        static void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker backgroundWorker = sender as BackgroundWorker;
            if(backgroundWorker != null)
            {
                while(!backgroundWorker.CancellationPending)
                {
 
                }
                if(backgroundWorker.CancellationPending)
                {
                    MessageBox.Show("Cancelled");
                    e.Cancel = true;

                }
            }
        }


In you progress form

C#
frmParent myParent;
        public frmProgress(frmParent myParent)
        {
            InitializeComponent();
            this.myParent = myParent;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            myParent.formWorker.CancelAsync();
        }


Hope this helps
 
Share this answer
 
Comments
All Time Programming 1-Jun-11 10:33am    
Wayne, Thanks but the bgworker is nover cancelled and it works normally completing its task. But throws an exception "Object reference ...." If I press ok on that error, the app quits.

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