BackgroundWorker se bloquea si se informa sobre el progreso
Frecuentes
Visto 658 veces
2
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.
2 Respuestas
3
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:
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
1
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.
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 c# backgroundworker or haz tu propia pregunta.
Are you sure that "w" is not null? - Philippe Leybaert
I think you need to show the code for your calculations. - Enigmativity
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. - Kevin GosseMore code or inner exception details please - Adil
Have you set bw.WorkerReportsProgressto true? Have you hooked event handler? - Renatas M.