Disposing resources at application closing

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Platform: WinCE 4.2, .NET CF, C#

I am working on a wizard based application which consists basically of an
"Engine" class which handles the creation/display/disposing of the various
"wizard" forms.
All the wizard forms are derived from the same base class which in turn is
derived from System.Windows.Forms.Form.

The problem I have noticed is that Engine class's Dispose method is not
getting called when the application is closed via the taskbar or the Debugger
is stopped.
I can solve this by adding an event handler for the Form.Closing event and
explicitly call the Engine.Dispose method which will work when the app is
shutdown via the taskbar but not when the Debugger is stopped. This is not
acceptable because the Engine.Dispose method is now called whenever a wizard
form is closed - which is not acceptable.
What I want is that when the applicaion is closed manually by the user or
when the Debugger is stopped that the Engine's Dispose method is called.

TIA,

Mark
 
The debugger stopping your application is a special case. Don't stop your
application that way and you won't have the problem. It is a brutal
termination that there is no point coding for.

For user termination you should handle shutdown operations (although all
memory/handles etc is freed when the process exits anyway) in the *main*
form's Closing event (if you don't have a main form please describe the
startup sequence of your app).

As an aside, wherever you have a dispose you should also have a finalizer
for backup...

Cheers
Daniel
 
Here is the app startup code (some code that is irrelevant to this post has
been removed):

static void Main()
{
CPUI.Engine engine = CPUI.Engine.Instance;

/*
* The frmWelcome is a static member of CPUI.Engine.
*/
CPUI.Engine.frmWelcome = new frmWelcome(this);
CPUI.Engine.frmWelcome.Init();

this.currentForm = CPUI.Engine.frmWelcome;

/*
* 1/28/05: In .NET CF the Application.Run method can only be
called
* with a System.Windows.Forms.Form derived class. This form
cannot
* be Disposed because the Application will shut down. As a
result,
* in this application as the user exits the "Welcome" screen and
* navigates to subsequent screens there will always be two
forms active
* - the "Welcome" form (which will be hidden so that only one
button is
* displayed on the taskbar) and the current form the user is
interacting
* with.
*/
Application.Run(CPUI.Engine.frmWelcome);

}
 
So everything I said in my previous reply applies. Let us know where your
specific problem is.

Cheers
Daniel
 
I am not clear what we mean by *main* form. Is this the form that is passed
as the argument to the Application.Run method or is it the form that is
active when the user shuts down the application?
 
I have added an event handler for the Form.Closing event in the frmWelcome
but it is only executed if the user closes the application when the
frmWelcome is the active form. When the user has navigated through the wizard
to any other form and then closes the application, the frmWelcome_Closing()
event handler is not called.
What I would like is an event handler that is always called when the user
has closed the application and only when the user has closed the application.
 
Right, there is something missing here. I cannot see how closing any form
other than the main form (as defined below) can terminate your application.

Can you zip and post a small reproducible sample including the steps you
take to close the application? I will have a look at it and get back to you.

Cheers
Daniel
 
I created a small test app to illustrate what is going on, and in doing so I
found a solution to the problem - which seems very obvious now...
If I put a Form.Closing event handler in all of my wizard forms and inside
each of the event handlers call Engine.Dispose then the Engine.Dispose method
will be called at any time the user closes the application.
What I ended up doing is creating a Form.Closing event handler in the wizard
form base class and in that method calling Engine.Dispose. This seems to work.
 
Back
Top