简体   繁体   中英

Task scheduler trigger when system boot in C#

I have created a task scheduler and set its trigger time fixed eg 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.

Thanks in Advance.

Code :------------------------------------------------------

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

                taskdDef.RegistrationInfo.Description = "Does something";
                taskdDef.RegistrationInfo.Documentation = "http://www.mysite.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);
            }
        }

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");
      }
   }
}

You can add it to the registry as a start task. See here .

2000, XP

Start -> Settings -> Control Panel -> Scheduled Tasks
Open the properties of the task.
Open the "Schedule" tab.
From the "Schedule Task" pull-down select "At System Startup".

Vista

Start -> Settings -> Control Panel -> Administrative Tools -> Task Scheduler
Open the properties of the task.
Open the "Triggers" tab, then edit or create a trigger.
From the "begin the task" pulldown select "At Startup".

你应该制作一个Windows 服务e。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM