Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i Start and Stop a Scheduled Task Programmatically by a button click?

What I have tried:

using Microsoft.Win32.TaskScheduler;

public static void StartTask()
{
  using (TaskService ts = new TaskService())
   {
      Task t = ts.GetTask(TaskName);
       if (t != null)
        {
           t.Run();
         }
     }
}

Not Responding...Please help.
Posted
Updated 23-Dec-20 8:45am

1 solution

If your tasks are in a folder use:
Task t = ts.GetTask(taskFolder + "\\" + TaskName);

An alternative would be using the schtasks commands | Microsoft Docs[^]
You can run it from C# with Process.Start()
 
Share this answer
 
v2
Comments
Member 14978771 24-Dec-20 9:40am    
No, The task is not in a folder.
I actually need to run the task via Task object itself.
also want to check the Task status too like:
=> if (task.State.Equals("Running"))
How can i check that if i use Process.Start()??
Thank you.
RickZeeland 25-Dec-20 13:15pm    
Don't think you can check for running state (like for a Windows Service).
You can use schtasks /query to find task names however, see:
https://superuser.com/questions/1034962/how-can-i-retrieve-the-status-of-a-scheduled-task-using-schtasks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900