使用 C# Task Scheduler 库结束任务

Use C# Task Scheduler library to End Task

我目前正在将此库用于 windows 任务调度程序 https://github.com/dahall/TaskScheduler

我想要的是结束运行任务。在我的研究中,我发现我可以通过这个代码片段 task.Definition.Settings.Enabled = false 来禁用任务。但是我没有找到任何方法来结束 运行 任务。谁能帮我解决这个问题?

你试过了吗:

task.Stop();

https://dahall.github.io/TaskScheduler/html/M_Microsoft_Win32_TaskScheduler_Task_Stop.htm

您可以使用以下方法检索所有任务:

new TaskService().AllTasks

我认为,您正在寻找的是 Task.Stop()RunningTask.Stop()(继承自 Task class)方法。显然,他们会立即停止已注册的任务。如source code所述:

System account users can stop a task, users with Administrator group privileges can stop a task, and if a user has rights to execute and read a task, then the user can stop the task. A user can stop the task instances that are running under the same credentials as the user account. In all other cases, the user is denied access to stop the task.

这就是你要找的吗?

task.Enabled = false 将立即禁用单个任务。 task.Definition.Settings.Enabled = false 用于在注册前禁用任务。

要终止 运行 任务,请使用:

if (task.IsActive && task.State == TaskState.Running) task.Stop();