简体   繁体   中英

How do I notify Windows Task Scheduler when my application fails?

I have a WPF application scheduled in Task Scheduler.

I want to notify the Task Scheduler when the application fails.

In Task Scheduler window, in section Task Status at the column Run Result , I always get Success , even when the app throws an internal exception.

I used Application.Current.Shutdown(1) on an attempt to notify a fail to Task Scheduler, but with I wasn't successful.

How can this be done?

The problem is in the design of Task Scheduler. As pointed out here:

How does Windows Task Scheduler in Win7 recognize a failed task?

which I've verified in testing

The Windows Task Scheduler does not examine the exit code or any other values when your task completes. You must handle any error processing within your own script or program.

If you look in the history for your scheduled task, you should see two events, and Action Completed, followed by a Task Completed. If you examine the Action Completed, it should look something like this:

Task Scheduler successfully completed task "\\test4" , instance "{a41adae0-a378-45f6-aadc-648d27852042}" , action "C:\\blah..blah\\Release\\WpfApplication1.exe" with return code 55.

As you can see, the application exited with a return code, but Task Scheduler still says success. The only solution I see is to handle this yourself by right clicking on the history entry and selecting "Attach task to this event...".

Or, you could run your application from a batch file, and have the batch file examine the exit code, and act accordingly. You would then use Task Scheduler to schedule the batch file instead of scheduling your WPF application directly.

Regarding returning an exit code from your WPF app, you may need to right click on the project properties in Visual Studio, and in the Applications tab, select Console Application for Output Type. In addition, use a release build in Task Scheduler rather than a debug build to ensure that your application's exit code is used, not something generated out of the added debug stuff. You can test to see if your app is properly generating an exit code by making this little batch file in the same folder as your exe file and running it (replacing your app's exe file name):

wpfapplication1.exe
echo %errorlevel%
pause

Your original code may successfully set the exit code, but Shutdown is a gentler exit, and may not exit immediately (or at all), as it will wait for threads etc. to exit gracefully. Environment.Exit will exit more forcefully.

To use Environment.Exit, you should specify an exit code other than the default value of 0 (which means success). You can do this using

Environment.Exit(someNumber)

Environment.Exit

You would need to have a global exception handler to do this for otherwise uncaught exceptions. This blog post gives more details: http://jrich523.wordpress.com/tag/task-scheduler/

Works with:

throw new exception

Instead of The operation completed successfully. (0x0) , it will show something like (0xE0434362)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM