GrayFullBuster
GrayFullBuster

Reputation: 1063

Using of backgroundworker issues need help to fix this

When I press btn_Connect the backgroundworker will but when it reaches it prompting the messagebox after i click ok on messagebox it will run again and display messagebox again and when I click btn_Connect again it will do the same and it will increment so the first click is twice the second click of the btn_Connect will display the messagebox thrice. How to fix this,

Here is my code:

private void testConnection()
    {
        backgroundWorker.ReportProgress(15);

        txt.createConnectionFile(txtServerName.Text, txtDatabase.Text, txtUserName.Text, txtPassword.Text);

        backgroundWorker.ReportProgress(30);

        cn.createConnection();

        backgroundWorker.ReportProgress(60);

        try
        {
            backgroundWorker.ReportProgress(80);

            cn.openConnection();

            MessageBox.Show("Connected!", "Connection Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        backgroundWorker.ReportProgress(100);
    }

    private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar.Value = e.ProgressPercentage;
    }

    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        testConnection();
    }
private void btnConnect_Click(object sender, EventArgs e)
    {
        progressBar.Visible = true;
        backgroundWorker.WorkerReportsProgress = true;
        backgroundWorker.WorkerSupportsCancellation = true;
        backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;
        backgroundWorker.DoWork += backgroundWorker_DoWork;

        backgroundWorker.RunWorkerAsync();
    }

Upvotes: 0

Views: 161

Answers (1)

heads5150
heads5150

Reputation: 7463

One thing I notice is that you keep wiring up the DoWork and RunWorkerCompleted events every time the button is clicked.

Based on the code above, I suspect that the first time you click the button it runs once, then the button becomes enabled again. The next time you click the button, it will run twice, next time three times, etc.

You need to wire up the events outside of the button click. In the OnLoad for instance.

If you can't do that replace the click code with

private void btnConnect_Click(object sender, EventArgs e)
    {
        progressBar.Visible = true;
        backgroundWorker.WorkerReportsProgress = true;
        backgroundWorker.WorkerSupportsCancellation = true;
        backgroundWorker.ProgressChanged -= backgroundWorker_ProgressChanged;
        backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;

        backgroundWorker.DoWork -= backgroundWorker_DoWork;
        backgroundWorker.DoWork += backgroundWorker_DoWork;

        backgroundWorker.RunWorkerAsync();
    }

Upvotes: 1

Related Questions