views:

672

answers:

2

Hi,

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...

+3  A: 

I'm guessing that on each Merge button click you are registering a new delegate. If the following code:

Worker.DoWork += new System.ComponentModel.DoWorkEventHandler(Worker_DoWork);
Worker.RunWorkerCompleted += new  System.ComponentModel.RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);

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.

Robert Wilczynski
Thanks..it worked....one more thing my progress bar is still running when the message box appears that "Files has been merged". How to disappear the progress bar before message box appear?
crazy_itgal
Did you make it work? I assume the problem was that in the worker completed event handler the code below the condition if (progress != null) responsible for closing the dialog doesn't execute (condition evaluates to false)?
Robert Wilczynski
+2  A: 

If your worker is a module level variable and you are appending to its invokation list every time the button clicks then that method will be invoked each time. Say you click 3 times, then this code Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted); will be executed 3 times and will be invoked 3 times. Set the Completed handler once when you instantiate the worker object.

Bah! Beaten to it!

Simon Wilson