0

I have a backgroundworker with a recursive method in the Do_Work Method. If an exception occurs in the revursive method, then i am not able to catch that exception. I have already tried getting it trough the RunWorkerCompleted Event, but that doesn't work either.

Backgroundworker

var worker = new BackgroundWorker();
worker.DoWork += Do_Work;
worker.RunWorkerCompleted += Work_Completed;
worker.WorkerReportsProgress = true;
worker.ProgressChanged += ProgressChanged;
worker.RunWorkerAsync();

Do_Work Method

public void Do_Work(object sender,DoWorkEventArgs e)
{
    for(int i = 0;i<10;i++)
    {
        RecursiveMethod();
    }
}

Ive tried putting try catches around pretty much everything. And ive tried getting the exception from the completed event.

public void Work_Completed(object sender, RunWorkerCompletedEventArgs e)
{
    if(e.error != null){Do_Something()}
}

When i run my code i dont get to the work completed event. If any exception occurs in the recursive method my application stops. Does anyone know why this is happening and how to prevent this?

Danil.T
  • 125
  • 1
  • 9

1 Answers1

1

Your question should be answered in this post: https://stackoverflow.com/a/258664/7337767

If you throw an exception inside another thread which you don't catch, the VS Debugger will break there. Thats why it's working when you don't attach the Debugger