Task scheduler trigger when system boot in C#

左心房为你撑大大i 提交于 2019-12-11 11:05:33

问题


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.

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

回答1:


Using the Task Scheduler Manager Library from CodePlex, 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");
      }
   }
}



回答2:


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




回答3:


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".




回答4:


You should make a Windows Service.



来源:https://stackoverflow.com/questions/10833015/task-scheduler-trigger-when-system-boot-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!