Achyut
Achyut

Reputation: 59

Use C# Task Scheduler library to End Task

I'm currently using this library for the windows task scheduler https://github.com/dahall/TaskScheduler

What I want is to end the running task. In my research, I found that I can able to disable the task by this code snippet task.Definition.Settings.Enabled = false. But I did not find any way to end the running task. Can anyone help me with this?

Upvotes: 0

Views: 1722

Answers (3)

Achyut
Achyut

Reputation: 59

task.Enabled = false will disable the single task immediately. task.Definition.Settings.Enabled = false is used to disable a task before registration.

To terminate a running task, use:

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

Upvotes: 0

Tropin Alexey
Tropin Alexey

Reputation: 776

I think, what you are looking is Task.Stop() and RunningTask.Stop()(inherited from Task class) methods. Obviously, they stops the registered task immediately. As described in 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.

Is that what you looking for?

Upvotes: 1

Noman_1
Noman_1

Reputation: 530

Have you tried:

task.Stop();

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

You can retrieve all tasks using:

new TaskService().AllTasks

Upvotes: 2

Related Questions