[英]Task scheduler trigger when system boot in C#
我创建了一个任务调度程序并将其触发时间设置为固定的,例如每天下午 5:00,但我想在系统启动或引导时触发该事件。 如果您有任何示例,请帮助我编写代码。
提前致谢。
代码 : - - - - - - - - - - - - - - - - - - - - - - - - ------
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);
}
}
使用来自 GitHub 的Task Scheduler Manager Library ,你可以写这个
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");
}
}
}
您可以将其作为启动任务添加到注册表中。 见这里。
2000,经验值
开始 -> 设置 -> 控制面板 -> 计划任务
打开任务的属性。
打开“计划”选项卡。
从“计划任务”下拉菜单中选择“系统启动时”。
远景
开始 -> 设置 -> 控制面板 -> 管理工具 -> 任务计划程序
打开任务的属性。
打开“触发器”选项卡,然后编辑或创建触发器。
从“开始任务”下拉菜单中选择“启动时”。
你应该制作一个Windows 服务e。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.