Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 "est4" , instance "{a41adae0-a378-45f6-aadc-648d27852042}" , action "C:lah..blahReleaseWpfApplication1.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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...