i950
asked on
TargetInvocationException: Exception thrown by the target of an invocation
Hello,
I'm often getting the following error when I'm clicking the X to exit my application while a backgroundWorker is doing some work:
TargetInvocationException
Exception thrown by the target of an invocation
How can I fix that?
Any help is appreciated.
I'm often getting the following error when I'm clicking the X to exit my application while a backgroundWorker is doing some work:
TargetInvocationException
Exception thrown by the target of an invocation
How can I fix that?
Any help is appreciated.
When program exits and background thread is still running, you need to stop this thread and then exit. Look at code fragments from my sample project.
void backWorker_DoWork(object sender, DoWorkEventArgs e)
{
// Get parameters
BackgroundWorker worker = sender as BackgroundWorker;
while (...) // some loop
{
// do background work here - your existing code
if ( worker.CancellationPending ) // test if cancelled
{
e.Cancel = true;
break; // exit immediately if asked to stop
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if ( ! backWorker.IsBusy )
return; // OK, form can be closed
// Cancel synchronously (ask and wait)
backWorker.CancelAsync(); // ask to stop
// Wait when it really exits
while ( backWorker.IsBusy )
{
System.Threading.Thread.Sl eep(100);
}
// now form can be closed
}
void backWorker_DoWork(object sender, DoWorkEventArgs e)
{
// Get parameters
BackgroundWorker worker = sender as BackgroundWorker;
while (...) // some loop
{
// do background work here - your existing code
if ( worker.CancellationPending
{
e.Cancel = true;
break; // exit immediately if asked to stop
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if ( ! backWorker.IsBusy )
return; // OK, form can be closed
// Cancel synchronously (ask and wait)
backWorker.CancelAsync(); // ask to stop
// Wait when it really exits
while ( backWorker.IsBusy )
{
System.Threading.Thread.Sl
}
// now form can be closed
}
If your are exiting the app, then may be you can Create a new Thread as a worker Thread and set it's IsBackGround property to true. So that when ever your application exits, it does not have to wait for the worker Threa.
But then you looses the funtionality of BackGroundWorker type.
Regards,
But then you looses the funtionality of BackGroundWorker type.
Regards,
ASKER
surajguptha,
I tried that but it didn't work.
AlexFM,
I tried your code but the program freezes when I'm exiting the app.
singhhome,
I don't understand. Maybe some code will help.
Thanks to all.
I tried that but it didn't work.
AlexFM,
I tried your code but the program freezes when I'm exiting the app.
singhhome,
I don't understand. Maybe some code will help.
Thanks to all.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It worked! But I don't like this line:
System.Threading.Thread.Sl eep(100);
Can I remove it? It's working without it.
System.Threading.Thread.Sl
Can I remove it? It's working without it.
Without this line, main thread continues to consume processor time waiting for worker thread. Sleep moves thread to waiting state, this allows to worker thread to activate and finish its work. If you think that 100 is too much, reduce this even to 0. Sleep(0) actually means: make thread switch. If backWorker.IsBusy still returns true, we need to switch to worker thread rather than continue polling loop.
That's what i don't understand. Why the main thread should wait for the termination of the worker Thread.
User has clicked on the close button, how long your application should wait for the worker Thread to terminate?
backWorker.CancelAsync(); depends on the Polling done by the worker code. In theory this can return only after the complete worker task is over
Your are exiting the application, so can it be considered safe to just terminate the worker Thread? Any resources being used by the worker Thread will be automatically released.
Regards,
User has clicked on the close button, how long your application should wait for the worker Thread to terminate?
backWorker.CancelAsync(); depends on the Polling done by the worker code. In theory this can return only after the complete worker task is over
Your are exiting the application, so can it be considered safe to just terminate the worker Thread? Any resources being used by the worker Thread will be automatically released.
Regards,
Well, if you don't understand, you can ask at experts-exchange.com :)
BackgroundWorker1.CancelAs
Hope it helps