can I write a C# program that - when run as a scheduled task - detects when Task Scheduler tries to stop it can I write a C# program that - when run as a scheduled task - detects when Task Scheduler tries to stop it windows windows

can I write a C# program that - when run as a scheduled task - detects when Task Scheduler tries to stop it


The proper way to handle this is to assume your program can die suddenly and without any chance to run code whatsoever (because, you know, it can -- let's say the .NET runtime has a bug that triggers a crash) and detect this outside the program, so you can do the mailing/logging from there. If you can brook a delay, you can still do this from the program itself: detect an unclean shutdown when the task next runs and then notify. This is tons more reliable than attempting to do cleanup/signaling while the process is going down (for whatever reason). This is especially true for console applications (if you're using those) because exiting those normally doesn't even run any finalizers, unless you write code for it (AppDomain.ProcessExit and Console.CancelKeyPress aren't enough, you have to go all the way to SetConsoleCtrlHandler). All in all, this does not make me have much hope for a clean exit that isn't performed by the application itself.

This does not answer the original question of whether you can detect a request to stop issued by Task Scheduler, and if so, how. I've tried to establish how that works, but I failed: if I run a console application under Task Scheduler that refuses to exit, it will merrily keep on running, even if I've configured it to be terminated after 10 seconds or 1 minute. (You can't set timeouts this short from the interface, but you can from the command line.) I did not test if the minimum time supported by Task Scheduler, 1 hour, works. I've also not tested if things fare differently when using an actual schedule, rather than a manually triggered task. If you manually end a task, though, it defiinitely just calls TerminateProcess and doesn't give you any chance for a clean exit -- that alone should be some motivation to not put your code for signaling failure in the task itself.