2

I have a C# Console application that runs under Windows Task Scheduler.

Should the application fail for what ever reason, I use "Exit Codes" to propagate back to Task Scheduler.

However, no matter what I try, Task Schedule logs always report "Task Completed Successfully".

I have tried the two defined exit strategies.. being...

Environment.Exit(hasError ? -1 : 0);

and

static int Main(string[] args)
{
    ... code ...
   return (hasError ? -1 : 0);
}

Any suggestions what I may be doing wrong

Running app in Windows Server 2012 Standard

Chris Hammond
  • 2,064
  • 5
  • 27
  • 53
  • The answer to your question can be found here: http://stackoverflow.com/questions/16969500/how-do-i-notify-windows-task-scheduler-when-my-application-fails – bill Jun 11 '15 at 14:30
  • If did read the one bill but the bit where it say **Task Scheduler successfully completed task "\test4" , instance "{a41adae0-a378-45f6-aadc-648d27852042}" , action "C:\blah..blah\Release\WpfApplication1.exe" with return code 55.** always says **with return code 0** – Chris Hammond Jun 11 '15 at 14:36
  • Did you verify that your code `(hasError ? -1 : 0)` isn't returning 0 even though you may be expecting -1 ? – bill Jun 11 '15 at 14:38
  • I've put test code in to just `return (64)` and it still says **with return code 0** – Chris Hammond Jun 11 '15 at 14:44
  • instead of `return(64)` try doing `Environment.Exit(64)` directly? Do you get the same return code when running the app manually? – bill Jun 11 '15 at 14:47
  • I just created a brand new console application and put `Environment.Exit(45)` as the only line of code and added it to Task scheduler, and I received the correct history with the correct return code. – bill Jun 11 '15 at 14:49
  • Same problem. **return code 0** – Chris Hammond Jun 11 '15 at 14:50
  • What O/S you running? – Chris Hammond Jun 11 '15 at 14:51
  • Server 2008, don't have a 2012 to test – bill Jun 11 '15 at 14:52
  • also... can you advise where the error code is visible... just in case i am looking in the wrong place – Chris Hammond Jun 11 '15 at 14:52
  • Join this chat: http://chat.stackoverflow.com/rooms/80297/so-30783682 – Black Frog Jun 11 '15 at 14:52
  • http://i.imgur.com/o515OOW.png is a screenshot. I'll assist you in the chat link Black Frog shared if you want further help – bill Jun 11 '15 at 14:56
  • @bill I can see in your screen shot that it shows the 45 exit code but mine ALWAYS says 0 no matter what exit code I specify – Chris Hammond Jun 12 '15 at 07:21
  • Create a brand new Console application and just put Environment.Exit(45); and then make that a scheduled task. If that doesn't work, then there is an issue with your server. – bill Jun 12 '15 at 13:21
  • Will try that next week bill... (Tuesday!) – Chris Hammond Jun 12 '15 at 17:35
  • Right... Done the test.... Two lines of code ... `File.WriteAllText("text.txt","Just Checking");` ... `Environment.Exit(45);` resulting in a text file being created and the following in the Task Scheduler "History" tab `Task Scheduler successfully completed task "xxxxxxxxxxx" , instance "{d18ea25c-2d1d-43d0-9485-8005776df35f}" , action "xxxxxx.exe" with return code 0.` – Chris Hammond Jun 16 '15 at 11:04
  • Tried the same programme on a Win 2003 server and it worked... Task Completion Status: 45.... So... Looks like a Win 2012 error then,, – Chris Hammond Jun 16 '15 at 11:27
  • OK... Interestingly the Last Run Result was "0x8007002D" === 2D = 45 But Eventlog says "0"... – Chris Hammond Jun 16 '15 at 11:38
  • Seems I have a possible answer.. http://stackoverflow.com/questions/22150922/return-code-of-scheduled-task-prefixed-with-0x8007000-in-list-view-registered-a – Chris Hammond Jun 16 '15 at 11:38
  • Well... it now shows "45" on both items... – Chris Hammond Jun 16 '15 at 13:02

1 Answers1

0

Turns out its a Windows 2012 Server issue and requires the following hot-fix

https://support.microsoft.com/en-us/kb/3003689

Chris Hammond
  • 2,064
  • 5
  • 27
  • 53