How to run code before program exit?

C#Exit

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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");
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFrankView Question on Stackoverflow
Solution 1 - C#GonzaloView Answer on Stackoverflow