[RESOLVED] Shutting down form with backgroundworker
Hi,
So I have this form with a backgroundworker component.
To properly clean up the form I tried calling backgroundWorker1.CancelAsync() in the Form_Closed event, but it seems to cancel that event. The form doesn't close.
Without everything is fine.
Can I assume the component cleans up its own threads at disposal?
Thanks :)
Re: Shutting down form with backgroundworker
In your code in the DoWork event, do you ever check for a pending cancellation?
If your code doesn't ever exit any loops or whatever then the thread will continue until it's done or externally terminated (without cleanup). I don't know if the BW component terminated it's threads when you dispose of it or not.
Re: Shutting down form with backgroundworker
The BackgroundWorker raises its DoWork event on a thread pool thread, which means a background thread. The one and only difference between a foreground thread and a background thread is that a background thread will not stop your process exiting, while a foreground thread will. That means that you can simply exit the app and the background task will be terminated.
That's OK if your background task can reasonably be terminated without any cleanup. If it can't then that's a problem. In that case you would have to stop the app exiting until your background task can respond to the request to cancel and clean up appropriately. In that case, you may be better off implementing your own threading and using a foreground thread instead.
Re: Shutting down form with backgroundworker
The form is a reconnection dialog that pauses the application while there is no connection to the server.
The backgroundworker's job is to check the connection (the check takes no more than a second).
It's fired by a timer every 5 seconds.
So basically the connection fails while the form is running.
When the user closes the dialog the application exits.
With the slight chance that the worker is canceled while connecting to the database I think the connection object is disposed when the form closes.
If not, one connection object stays in memory?
Guess it's running fine then.
Thanks for the suggestions!