Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hey im using selenium and backgroundworker but when i start my program and press start i can't press cancel button or stop button i don't have access to the form until the progress finished here is my simple code im testing in
<pre>using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;


namespace BgWorker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void startBtn_Click(object sender, EventArgs e)
        {
            //START BG WORKER
            backgroundWorker1.RunWorkerAsync();
            var dr = new ChromeDriver();
            dr.Navigate().GoToUrl("http://google.com");


        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            //REQUEST CANCELLATION
            backgroundWorker1.CancelAsync();
        }

      
        //RUN BG STUFF HERE.NO GUI HERE PLEASE
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i <= 100; i++)
            {
                //CHECK FOR CANCELLATION FIRST
                if (backgroundWorker1.CancellationPending)
                {
                    //CANCEL
                    e.Cancel = true;
                }
                else
                {
                    simulateHeavyJob();
                    backgroundWorker1.ReportProgress(i);
                }
            }

        }

        //THIS UPDATES GUI.OUR PROGRESSBAR
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            percentageLabel.Text = e.ProgressPercentage.ToString() + " %";
        }

        //WHEN JOB IS DONE THIS IS CALLED.
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                display("You have Cancelled");
                progressBar1.Value = 0;
                percentageLabel.Text = "0";
            }
            else
            {
                display("Work completed successfully");
            }
        }
        //SIMULATE HEAVY JOB
        private void simulateHeavyJob()
        {
            //SUSPEND THREAD FOR 100 MS
            Thread.Sleep(100);
        }
        //DISPLAY MSG BOX
        private void display(String text)
        {
            MessageBox.Show(text);
        }
        

       
       
    }
}


What I have tried:

using backgroundworker while selenium c#
Posted
Comments
Ravi Bhavnani 22-Jun-17 20:42pm    
Please show you construct and setup the background worker.

/ravi
Ilyas Zahir 22-Jun-17 20:50pm    
im sorry i didn't understand
Ravi Bhavnani 22-Jun-17 20:56pm    
Have you set the worker's WorkerReportsProgress and WorkerSupportsCancellation properties to true?

/ravi
Ilyas Zahir 22-Jun-17 21:15pm    
yes i did
Ravi Bhavnani 22-Jun-17 21:21pm    
Apart from the incorrect loop condition (should be i < 100, not i <= 100), your code seems OK to me. Are you able to cancel the background worker if you comment out the Chrome driver stuff?

/ravi

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