Executing a program before System Shutdown

0

I have a program I need to run before a system shutdown executes. This means, before any program gets terminated because of the shutdown, I need to execute said program, ideally via Process.Start().

Initially I tried executing said program in the Windows Shutdown Scripts, however that script is only executed after programs have already been terminated. I however need to terminate some running applications in the right order.

I have tried the event SystemEvents.SessionEnding and SessionEnded, but this event does not let you wait for more than 1-2 seconds, and the cancelling of the shutdown is not possible either.

I got up to this example where, in the WndProc event about shutdown I would execute a Process. I reach the Event, I reach the point where I can actually pause the shutdown process. However, when I try to actually call a process, I get the "0xC0000142" error. A search for this resulted in one option being that the user is incorrect, however I want to call this application with the user that is running the executing program. The other option was that the environment path was wrong. I set an environment path for the second option, but in my opinion, I shouldn't need to change anything for the first option, since I want to execute it with the current user.

A third possibility I thought about might be that at this state of the system shutdown, no new processes can be spawned.

What can I do to fix this and actually execute the program here? I seem to be out of ideas at this point.

Clarification: The "cleanup" program needs to shut down programs in a given order. The programs to be shut down are our own, but Windows Forms (the only way I can see that can use a shutdown handler) cannot be used there. Program A is the one that handles the OnShutdown. Program A, on Shutdown, will call Program B, which does the ordered closing of programs. Program B, in my solution (that includes Forms) cannot be launched and returns an error, as specified in paragraph 4. The only user interaction here would be a user pressing a power button leading to system shutdown, or a remotely executed "shutdown.exe" call.

c#
windows
shutdown
asked on Stack Overflow Apr 5, 2016 by private_meta • edited May 23, 2017 by Community

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0