I ran into a variation of the problem I asked about here. Instead of a class extending System.Windows.Form, I have a class extending System.Windows.UserControl, which doesn’t have the FormClosing event.
I thought about adding a Close method to the user control, in which I tell the native thread to stop. When it stops, it raises an event to the user control, which calls Dispose on itself. The problem with this approach is that calling Dispose from the object to dispose (suicide?) seems a bad practice, and I had to write a finalizer that does nothing (to avoid double disposal), which feels even worse…
Suggestions?
Update: My user control lives inside a class which extends System.Windows.Forms.ToolStripControlHost, and an instance of that class lives in the main application form.
I would add some sort of method to the user control and call it in the forms
FormClosingevent…The method in the user control would be blocking so that it didn’t return until the disposal was complete.
But, you could cancel the closing in the Closing event, disable the form, do something in the background and when done, close the form from code. For example: