BackgroundWorker se bloquea si se informa sobre el progreso

I have a BackgroundWorker that calls a method in a class. The method is given a reference to the BackgroundWorker so that it can report back its progress. This pseudocode shows the outline:

void BackgroundWorker_DoWork( object sender )
{
    BackgroundWorker w = sender as BackgroundWorker;
    var c = new ProcessingClass()
    c.someMethod( w )
}

class ProcessingClass()
{
    someMethod( BackgroundWorker w )
    {
        w.ReportProgress(50)
        //calculations...
    }
}

The problem is that when doing the "calculations" after reporting the progress, the program will crash. the debugger jumps to Application.Run( new frmMain() ); in the startup "Program" class, reason given "Exception has been thrown by the target of an invocation."

If I don't report the progress, everything works fine. Does anyone know why? My guess is that this has something to do with threads, but I don't see how.

preguntado el 28 de agosto de 12 a las 09:08

Are you sure that "w" is not null? -

I think you need to show the code for your calculations. -

It could be an error in your ProgressChanged event handler. If you empty it, does it work? Also, having the type of the exception and the full stack trace would definitely help. -

More code or inner exception details please -

Have you set bw.WorkerReportsProgressto true? Have you hooked event handler? -

2 Respuestas

Solo algunas sugerencias:

1.) Inspect the InnerException perfecta de los TargetInvocationException excepción.

2.) Configure Visual Studio to stop as soon as the exception is thrown:

enter image description here

This should help getting closer to the error.

Respondido 28 ago 12, 09:08

Thanks, this helped me find the exact place and solve the problem! Here's the solution: ProgressChange read from UserState which I had not set, causing the error. - rlv-dan

Maybe you assume that ReportProgress is blocking and prepares something on which the code in // calculations ... depends upon. Not that it's no blocking:

The call to the ReportProgress method is asynchronous and returns immediately.

http://msdn.microsoft.com/en-us/library/ka89zff4.aspx

Respondido 28 ago 12, 09:08

Thanks. I wasn't aware of this, but it's not the problem in this case. - rlv-dan

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.