Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task scheduler trigger when system boot in C#

I have created a task scheduler and set its trigger time fixed e.g. daily-5:00 pm, but I want to trigger that event when system start or boot. Please help me with code if you have any example.

My tried code:

public static void CreateTask()
{
    using (TaskService task = new TaskService())
    {
        TaskDefinition taskdDef = task.NewTask();

        taskdDef.RegistrationInfo.Description = "Does something";
        taskdDef.RegistrationInfo.Documentation = "http://www.example.com";

        taskdDef.Settings.ExecutionTimeLimit = new TimeSpan(0, 10, 0);
        taskdDef.Settings.AllowDemandStart = true;

        taskdDef.Actions.Add(new ExecAction(@"D:\Myfolder\bin\SGSclient.exe", 
            "yourArguments", null));
        task.RootFolder.RegisterTaskDefinition("YourTask", taskdDef);
    }
}
like image 296
manjinder singh Avatar asked Jul 30 '26 17:07

manjinder singh


1 Answers

Using the Task Scheduler Manager Library from GitHub, you could write this

using System;
using Microsoft.Win32.TaskScheduler;

class Program
{
   static void Main(string[] args)
   {
      // Get the service on the local machine
      using (TaskService ts = new TaskService())
      {
         // Create a new task definition and assign properties
         TaskDefinition td = ts.NewTask();
         td.RegistrationInfo.Description = "Does something";

         // Create a trigger that will fire after the system boot
         td.Triggers.Add(new BootTrigger() );

         // Create an action that will launch Notepad whenever the trigger fires
         td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));

         // Register the task in the root folder
         ts.RootFolder.RegisterTaskDefinition(@"Test", td);

         // Remove the task we just created
         ts.RootFolder.DeleteTask("Test");
      }
   }
}
like image 185
Steve Avatar answered Aug 01 '26 05:08

Steve