0

Im having an issue where i need to get my Classes to run a piece of code on exit. Basically the code writes the Property's and Parameters to an XML file so they can be sent to the programmer to replicate the same settings as the client.

so i have created code like this on each of my classes.

    ~WorkspaceViewModel()
    {
        this.Save("Workspace");
    }

my problem is that i cannot find a handler that will run before this destructor. i have tried the following

        //App.Current.Exit += new System.Windows.ExitEventHandler(ProgramExit);
        //AppDomain.CurrentDomain.ProcessExit += new EventHandler(ProgramExit);
        //App.Current.MainWindow.Closed += new EventHandler(ProgramExit);
        //App.Current.Windows[0].Closed += new EventHandler(ProgramExit);
        //AppDomain.CurrentDomain.DomainUnload += new EventHandler(ProgramExit);
        //App.Current.MainWindow.Unloaded += new System.Windows.RoutedEventHandler(ProgramExit);
        //System.Windows.Forms.Application.ApplicationExit += new EventHandler(ProgramExit);
        //System.Windows.Application.Current.Exit += new System.Windows.ExitEventHandler(ProgramExit);

And saw something online about modifying the App class so i did this.

public partial class App : System.Windows.Application
{
    public void OnExit()
    {
        this.OnExit();
    }
    public void App_Exit(Object sender, System.Windows.ExitEventArgs Args)
    {
        //Somelogic here
    }
    public App()
    {
        this.Exit += new System.Windows.ExitEventHandler(App_Exit);
    }
}

could someone please help.

New Bee
  • 991
  • 1
  • 13
  • 24

3 Answers3

0

Are you using Windows Forms? If so, you can use the Closing event of the form. More reading: Form.Closing Event

Definition:

Occurs when the form is closing.

Example:

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    if(MessageBox.Show("Do you want to exit?", "Your app title", MessageBoxButtons.YesNo) ==  DialogResult.No)
    {
        e.Cancel = true;
        // cancel the closing
    }
    //otherwise the application will exit
}

You don't really need the if-statement, you can just call the Save method in this event and let the application exit afterwards.

Abbas
  • 14,186
  • 6
  • 41
  • 72
0

You could do it this way (pseudocode)

Init();
window.Show();
Deinit();

You explicitly run initializers on application start and deinitiliazer on exit.

And in WPF it's done by using Application events, overrides or by a trick (setting Page build action for App.xml). In your implementation, you can't have constructor declared, because it's already generated (file App.g.i.cs). You can use application startup event though or simply set events in xaml.

Community
  • 1
  • 1
Sinatr
  • 20,892
  • 15
  • 90
  • 319
0

As it turns out. all i needed to do was create a destructor on the App Class like this

public partial class App : Application
{
    ~App()
    {
        Administration.Model.DataBaseModel.GlobalCatalogue.ToFile();
    }
}

Not really sure this is the best approach tho

Im still open to better ideas however.. thank you all.

New Bee
  • 991
  • 1
  • 13
  • 24
  • A destructor is **never** an appropriate "exit handler". Finalization occurs in an unpredictable order. Your code will randomly crash at a *very* awkward moment when you try to save an object that was already finalized. All of the frameworks that have an "Application" class provide an event that tells you when the app is exiting. Nobody can tell which framework you are using. – Hans Passant May 06 '14 at 11:44
  • 1
    its nice that you tell me this. how about providing an alternative. – New Bee May 06 '14 at 23:10