我如何通知Windows计划任务时,我的应用程序失败? [英] How do I notify Windows Task Scheduler when my application fails?

查看:138
本文介绍了我如何通知Windows计划任务时,我的应用程序失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有计划在任务计划程序WPF应用程序。



我要当应用程序未通知任务计划程序。



在任务计划窗口,在该列运行结果部分任务状态,我总是成功,即使该应用程序会引发内部异常。



我用 Application.Current。关机(1)如果试图通知未能任务计划程序,但我没有成功。



如何才能做到这一点?


解决方案

您应该指定比默认值0以外的退出代码(这意味着成功)。为此,您可以使用



  Environment.Exit(someNumber)

Environment.Exit



您需要有一个全球性的异常处理程序,否则捕获的异常做到这一点。本博客文章提供了更多的细节: http://jrich523.wordpress.com/tag/task-scheduler/



修改



我做了一个假设的问题是在应用程序中。你知道他们说的是什么的假设。这个问题,原来是在任务计划程序的设计。这里指出的:



如何Windows计划任务在Win7认识​​到失败的任务?



这是我在测试你的任务完成时,


Windows任务计划程序不检查退出代码或任何其他
值。您必须在自己的脚本或程序中处理任何错误处理




如果您在历史上寻找你的计划任务,你应该看到两个事件,并完成了动作,随后完成的任务。如果您检查已完成的动作,就应该是这个样子:




任务计划程序成功完成任务\test4,比如
{a41adae0-a378-45f6-AADC-648d27852042}行动
C:\blah..blah\Release\WpfApplication1.exe,返回码55




正如你所看到的,应用程序退出,返回代码,但任务计划仍表示成功。我看到的唯一的解决方法是用鼠标右键点击的历史记录条目来处理这个自己,并选择附加任务,此事件......



或者,你可以运行从一个批处理文件您的应用程序,并拥有该批处理文件检查退出代码,并采取相应的行动。那么你可以使用任务计划程序安排批处理文件,而不是直接调度WPF应用程序。



关于从WPF应用程序返回的退出代码,您可能需要右键点击在Visual Studio中的项目属性,并在应用程序选项卡,选择控制台应用程序的输出类型。此外,使用任务计划程序的发布版本,而不是调试版本,以确保您的应用程序的退出代码时,出的附加调试的东西产生的不是。您可以测试,看看你的应用程序是正确通过在同一个文件夹这个小批量文件作为exe文件并运行它生成退出代码(更换您的应用程序的EXE文件名):

  wpfapplication1.exe 
回声%ERRORLEVEL%
暂停

您原来的代码可以成功设置退出代码,但关机是一个温和的退出,可能不会立即(或全部)退出,因为它会等待线程等正常退出。 Environment.Exit将退出更有力。


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?

解决方案

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/

EDIT

I made an assumption that the problem was in your application. You know what they say about assumptions. The problem, it turns out 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.

这篇关于我如何通知Windows计划任务时,我的应用程序失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆