0

I have the following simple code:

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Process pr = Process.GetCurrentProcess();
            pr.EnableRaisingEvents = true;
            pr.Exited += HandleClosed;
            Console.ReadKey();

        }

        private static void HandleClosed(object sender, EventArgs e)
        {

            File.Create(@"C:\MYDIR\myfile.c");
        }
    }
}

where when the console app exited I want to create the file specified in the HandleClosed event. However the event Exited is never raised even if I specified EnableRaisingEvents to true. Any suggestion?

P.S. MSDN says:

The Exited event indicates that the associated process exited. This occurrence means either that the process terminated (aborted) or successfully closed. This event can occur only if the value of the EnableRaisingEvents property is true.

There are two ways of being notified when the associated process exits: synchronously and asynchronously. Synchronous notification means calling the WaitForExit method to block the current thread until the process exits. Asynchronous notification uses the Exited event, which allows the calling thread to continue execution in the meantime. In the latter case, EnableRaisingEvents must be set to true for the calling application to receive the Exited event.

When the operating system shuts down a process, it notifies all other processes that have registered handlers for the Exited event. At this time, the handle of the process that just exited can be used to access some properties such as ExitTime and HasExited that the operating system maintains until it releases that handle completely.

so it seems possible to detect that event even if the app is exited!

xdevel2000
  • 20,780
  • 41
  • 129
  • 196
  • 1
    I'm not sure about that, but doesn't the Exited event raise after the process is closed? You cannot catch the event because the time it would've been risen, your application already exited. – Timo Jan 28 '18 at 17:21
  • 2
    You're asking the program to give a eulogy at it's own funeral. – Rotem Jan 28 '18 at 17:22
  • Possible duplicate: https://stackoverflow.com/q/4646827/6205379 – Timo Jan 28 '18 at 17:23
  • @Timo, I agree with you but the MSDN doc says differently. I have upgraded my question... – xdevel2000 Jan 28 '18 at 17:30
  • Your current process hasn't exited successfully, how do you want to catch? Isn't it an egg and hen problem? – Ali Bahrami Jan 28 '18 at 17:33
  • @AliBahraminezhad, the process, as the MSDN says, must be notified also if aborted and also if I hitted the exit button... and so on... on every event that exited the app – xdevel2000 Jan 28 '18 at 17:36
  • It says "This occurrence means either that the process terminated (aborted) or successfully closed." ... `closed` not `closing` or `being closed`. I think closed means it completely closed. – Ali Bahrami Jan 28 '18 at 17:37
  • 1
    I think the problem is that your handle to the event is released before. When your application exits, the event handle is released (so you are not subscribed to the event anymore). Then your app closes. After that, Windows invokes all subscribed event handlers because the process exited. Unfortunately, your handle is already released because your app already exited. If you interpret it that way, the MSDN doc is still valid. – Timo Jan 28 '18 at 17:38
  • ... but then how can I intercept when a process is closing??? there is something I don't understand... – xdevel2000 Jan 28 '18 at 17:43
  • 2
    If it's any other process than the process that runs your code, you are good to go with your current implementation. In your case, take a look at this answer https://stackoverflow.com/a/4647168/6205379 – Timo Jan 28 '18 at 17:47

1 Answers1

1

As all the comments suggest you cannot handle the Exited of your application from within the same application (Exited implies that it has already happened and so there is no more user code to execute)

However, there are ways of handling this from within a console application as suggested here.

Scott Perham
  • 2,410
  • 1
  • 10
  • 20