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(); }