I have a WPF application scheduled in Task Scheduler.
我在任务计划程序中安排了WPF应用程序。
I want to notify the Task Scheduler when the application fails.
我想在应用程序失败时通知Task Scheduler。
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.
我使用Application.Current.Shutdown(1)尝试通知任务计划程序失败,但是我没有成功。
How can this be done?
如何才能做到这一点?
1 个解决方案
#1
14
You should specify an exit code other than the default value of 0 (which means success). You can do this using
您应指定默认值0以外的退出代码(表示成功)。你可以这样做
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/
您需要有一个全局异常处理程序来执行此操作以防止未捕获的异常。此博客文章提供了更多详细信息: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:
我假设问题出在你的申请中。你知道他们对假设的看法。问题,原来是在Task Scheduler的设计中。正如这里指出的:
How does Windows Task Scheduler in Win7 recognize a failed task?
Win7中的Windows任务计划程序如何识别失败的任务?
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.
任务完成时,Windows任务计划程序不会检查退出代码或任何其他值。您必须在自己的脚本或程序中处理任何错误处理。
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:
如果查看计划任务的历史记录,则应该看到两个事件,“操作已完成”,然后是“任务已完成”。如果您检查Action Completed,它应该如下所示:
Task Scheduler successfully completed task "\test4" , instance "{a41adae0-a378-45f6-aadc-648d27852042}" , action "C:\blah..blah\Release\WpfApplication1.exe" with return code 55.
任务计划程序成功完成了任务“\ test4”,实例“{a41adae0-a378-45f6-aadc-648d27852042}”,操作“C:\ blah..blah \ Release \ WpfApplication1.exe”,返回码为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...".
如您所见,应用程序退出并返回代码,但Task Scheduler仍然表示成功。我看到的唯一解决方案是通过右键单击历史记录条目并选择“将任务附加到此事件...”来自行处理。
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.
或者,您可以从批处理文件运行应用程序,并让批处理文件检查退出代码,并采取相应措施。然后,您将使用任务计划程序来计划批处理文件,而不是直接计划您的WPF应用程序。
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):
关于从WPF应用程序返回退出代码,您可能需要在Visual Studio中右键单击项目属性,然后在“应用程序”选项卡中,选择“输出类型的控制台应用程序”。此外,在任务计划程序中使用发布版本而不是调试版本来确保使用应用程序的退出代码,而不是使用添加的调试内容生成的内容。您可以通过将此小批处理文件放在与exe文件相同的文件夹中并运行它(替换应用程序的exe文件名)来测试您的应用程序是否正确生成退出代码:
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.
您的原始代码可能会成功设置退出代码,但Shutdown是一个更温和的退出,并且可能不会立即退出(或根本不退出),因为它将等待线程等正常退出。 Environment.Exit将更有力地退出。
#1
14
You should specify an exit code other than the default value of 0 (which means success). You can do this using
您应指定默认值0以外的退出代码(表示成功)。你可以这样做
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/
您需要有一个全局异常处理程序来执行此操作以防止未捕获的异常。此博客文章提供了更多详细信息: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:
我假设问题出在你的申请中。你知道他们对假设的看法。问题,原来是在Task Scheduler的设计中。正如这里指出的:
How does Windows Task Scheduler in Win7 recognize a failed task?
Win7中的Windows任务计划程序如何识别失败的任务?
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.
任务完成时,Windows任务计划程序不会检查退出代码或任何其他值。您必须在自己的脚本或程序中处理任何错误处理。
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:
如果查看计划任务的历史记录,则应该看到两个事件,“操作已完成”,然后是“任务已完成”。如果您检查Action Completed,它应该如下所示:
Task Scheduler successfully completed task "\test4" , instance "{a41adae0-a378-45f6-aadc-648d27852042}" , action "C:\blah..blah\Release\WpfApplication1.exe" with return code 55.
任务计划程序成功完成了任务“\ test4”,实例“{a41adae0-a378-45f6-aadc-648d27852042}”,操作“C:\ blah..blah \ Release \ WpfApplication1.exe”,返回码为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...".
如您所见,应用程序退出并返回代码,但Task Scheduler仍然表示成功。我看到的唯一解决方案是通过右键单击历史记录条目并选择“将任务附加到此事件...”来自行处理。
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.
或者,您可以从批处理文件运行应用程序,并让批处理文件检查退出代码,并采取相应措施。然后,您将使用任务计划程序来计划批处理文件,而不是直接计划您的WPF应用程序。
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):
关于从WPF应用程序返回退出代码,您可能需要在Visual Studio中右键单击项目属性,然后在“应用程序”选项卡中,选择“输出类型的控制台应用程序”。此外,在任务计划程序中使用发布版本而不是调试版本来确保使用应用程序的退出代码,而不是使用添加的调试内容生成的内容。您可以通过将此小批处理文件放在与exe文件相同的文件夹中并运行它(替换应用程序的exe文件名)来测试您的应用程序是否正确生成退出代码:
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.
您的原始代码可能会成功设置退出代码,但Shutdown是一个更温和的退出,并且可能不会立即退出(或根本不退出),因为它将等待线程等正常退出。 Environment.Exit将更有力地退出。