wpf 当我的应用程序失败时,如何通知 Windows 任务计划程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16969500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:57:01  来源:igfitidea点击:

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

c#wpfscheduled-tasks

提问by Alexandre Perez

I have a WPF application scheduled in Task Scheduler.

我在任务计划程序中安排了一个 WPF 应用程序。

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

我想在应用程序失败时通知任务计划程序。

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

在 Task Scheduler 窗口中,在Task Status列的部分中,即使应用程序引发内部异常Run Result,我也总是得到Success

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?

如何才能做到这一点?

回答by hatchet - done with SOverflow

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

问题在于任务计划程序的设计。正如这里所指出的:

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 Completed。如果您检查 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...".

如您所见,应用程序以返回码退出,但任务计划程序仍显示成功。我看到的唯一解决方案是通过右键单击历史条目并选择“将任务附加到此事件...”来自己处理。

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 中的项目属性,然后在“应用程序”选项卡中,为“输出类型”选择“控制台应用程序”。此外,在 Task Scheduler 中使用发布版本而不是调试版本来确保使用您的应用程序的退出代码,而不是从添加的调试内容中生成的内容。您可以通过将这个小批处理文件与您的 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 会更有力地退出。

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,您应该指定除默认值 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/