How to Run Code Before Program Exit

How to run code before program exit?

Try the ProcessExit event of AppDomain:

using System;
class Test {
static void Main(string[] args)
{
AppDomain.CurrentDomain.ProcessExit += new EventHandler (OnProcessExit);
// Do something here
}

static void OnProcessExit (object sender, EventArgs e)
{
Console.WriteLine ("I'm out of here");
}
}

Doing something before program exit

Check out the atexit module:

http://docs.python.org/library/atexit.html

For example, if I wanted to print a message when my application was terminating:

import atexit

def exit_handler():
print 'My application is ending!'

atexit.register(exit_handler)

Just be aware that this works great for normal termination of the script, but it won't get called in all cases (e.g. fatal internal errors).

Run code before exiting a C# window application

Here is a way to control both opening and closing actions in WPF.

Under App.xaml, open App.xaml.cs and change as follows.

namespace WpfApp1
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private void App_Startup(object sender, StartupEventArgs e)
{
// Code for before window opens (optional);

var mainWindow = new MainWindow();
mainWindow.Show();

mainWindow.Closed += Window_Closed;
}

private void Window_Closed(object sender, EventArgs e)
{
// Code for after window closes goes here.
MessageBox.Show("Goodbye World!");
}
}
}

And change App.xaml as follows to use:

<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
Startup="App_Startup" >
<Application.Resources>

</Application.Resources>
</Application>

The key change here is Startup="App_Startup".

These make it easier to understand exactly when each action is occurring, as it makes the entry and exit points clearer.

Note: This will not work if you want the user to be able to cancel quitting the app, but from your example code that is not what you are looking for.

How do I run certain code/function before the program exits in D?

You could use core.stdc.signal, which contains bindings to the C header signal.h. Now, if this is for Windows, you might run into some problems:

SIGINT is not supported for any Win32 application. When a
CTRL+Cinterrupt occurs, Win32 operating systems generate a new thread to
specifically handle that interrupt. This can cause a single-thread
application, such as one in UNIX, to become multithreaded and cause
unexpected behavior.

__gshared bool running = true;
extern(C) void handleInterrupt(int) nothrow @nogc
{
running = false;
}

void main()
{
import core.stdc.signal;
signal(SIGINT, &handleInterrupt);

scope(exit)
{
//Cleanup
import std.stdio : writeln;
writeln("Done");
}

while(running)
{
//Do some work
}
}

Running code on program exit in Java

Use Runtime.getRuntime().addShutdownHook(Thread).

C: Doing something when the program exits

look into the atexit API of the C standard library.



Related Topics



Leave a reply



Submit