Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run code before program exit? [duplicate]

Tags:

c#

exit

I have a little console C# program like

Class Program 
{ 
    static void main(string args[]) 
    {
    }
}

Now I want to do something after main() exit. I tried to write a deconstructor for Class Program, but it never get hit.

Does anybody know how to do it.

Thanks a lot

like image 404
Frank Avatar asked Mar 31 '10 18:03

Frank


1 Answers

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");
    }
}
like image 147
Gonzalo Avatar answered Sep 22 '22 06:09

Gonzalo