0

I'm new to AWS. I'm writing a console application that will be long running on a Windows EC2 instance. The application starts up and does a Console.ReadLine() it runs a long running background task.

What I'd like to do is do some clean up work when the EC2 instance is shut down. Is there any way to set up EC2 to send a new line command to the console application and then wait to get an application exit code before continuing the shutdown so my application can finish whatever work it is doing and shutdown gracefully?

James
  • 523
  • 1
  • 4
  • 20

1 Answers1

0

I did some googling for you. Looks like you can subscribe to ProcessExit event. Basically before your process exit, you can do some last minute activities.

using System;
class Program
{
    static void Main(string[] args)
    {
        AppDomain.CurrentDomain.ProcessExit += (s, e) => Console.WriteLine("Process exiting");
        Environment.Exit(0);
    }
}

reference: How to call event before Environment.Exit()?

Arun Kamalanathan
  • 8,107
  • 4
  • 23
  • 39