I have a listbox in which i have to give minimum 2 files for merging.
the merging is done when i click Merge button.The progress bar starts and the message box appears That the files has been merged.i am using background worker to run the progress bar.
Now the problem is when the merging is done with 2 files,i add one more file,Click the merge button merging is done message appears i click OK on the message box,again the message box appears with same message that merging has been done.This message box continues appearing the number of times i add the file in the listbox .
For example, for 2 files message appears 1ce den on adding 1 more file message appears 2ice ,1 more file in listbox message appears 3ic.Like dis it continues….
When i used the debugger to track it, i noticed that my Background Worker Runcompleted event is called that number of times whenever i add file in the listbox.Here is the code for Merge button Click event…
Worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
Worker.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);
Worker.WorkerSupportsCancellation = true;
if (!Worker.IsBusy)
Worker.RunWorkerAsync();
else
MessageBox.Show("Cannot run background worker twice ");
if (Worker.IsBusy)
{
progress = new ProgressDialogDTB();
progress.FormClosing +=
new FormClosingEventHandler(ProgressDialog_FormClosing);
progress.ShowDialog(this);
}
while (Worker.IsBusy)
{
Application.DoEvents();
}
//For Background Worker completed Event...
private void Worker_RunWorkerCompleted(object sender,
AsyncCompletedEventArgs e)
{
if (progress != null)
{
progress.Close();
progress = null;
}
if ( e.Cancelled )
MessageBox.Show(" Progress was cancelled ");
if (e.Error == null)
if (!e.Cancelled)
MessageBox.Show("Files has been merged ");
if (e.Error != null)
MessageBox.Show(e.Error.Message);
}//Worker_RunWorkerCompleted
I don't know where i am getting wrong.
Please help…Thanks…
Best Answer
I'm guessing that on each Merge button click you are registering a new delegate. If the following code:
is in the click event handler of the Merge button consider moving it to your form initailization method. You only need to register a delegate once. Next time you do it adds a new one so on second Merge click it will run twice, on third click three times and so on.