Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BackgroundWorker component problem 1

Status
Not open for further replies.

captainPlus

Programmer
Apr 15, 2007
41
TR
Hi,

I'm using BackgroundWorker component for a time-consuming operation. Everything works fine, and the job is done correctly. But my problem is, BackgroundWorker component's RunWorkerCompleted event is fired several times.

When I use BackgroundWorker component for the first time, it only fires one time, this is ok. But when I run it a second time, it fires RunWorkerCompleted event two times. And three times on third run, and so on...

What am I doing wrong? Any help will be really great..

Thanks...
@
 
Can you post relevant portion of teh code you're having trouble with?

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Here is the part of code... bwTestConnection is the BackgroundWorker object.

After clicking btnTestConnection, bwTestConnection_DoWork succesfully starts. And after completion of async job, RunWorkerCompleted triggers more than once after the first click...

Thanks;

Code:
        private bool lastTestResult = false;
        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            if (txtConnStr.Tag != null)
            {
                errProv.SetError(txtConnStr, null);
                btnTestConnection.Visible = false;
                pbTestConnection.Visible = true;
                bwTestConnection.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwTestConnection_RunWorkerCompleted);
                bwTestConnection.RunWorkerAsync();
            }
            else
            {
                errProv.SetError(txtConnStr, "Connection string olu?turulmad?!");
            }
        }

        void bwTestConnection_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
                if (!bwTestConnection.IsBusy)
                {
                    btnTestConnection.Visible = true;
                    pbTestConnection.Visible = false;
                    if (lastTestResult)
                        MessageBox.Show("Veritaban?yla ba?lant? ba?ar?yla sa?lanm??t?r!\nSistemi bu parametreler ile konfigüre edebilirsiniz...", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                        MessageBox.Show("Belirtilen veritaban? ile gerekli ba?lant? sa?lanamam??t?r!", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                
        }

        private void bwTestConnection_DoWork(object sender, DoWorkEventArgs e)
        {
            TestConnection();
        }
 
you're assigning a new event handler EACH time you click on the button

Code:
bwTestConnection.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwTestConnection_RunWorkerCompleted);

move it out of the click block and call it from initialization.

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top