我想创建每2分钟触发一次的任务计划程序.我正在使用以下namesapce
使用Microsoft.Win32.TaskScheduler
我写了以下代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32.TaskScheduler;
namespace SchedulerTest1
{
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 the task at this time every other day
td.Triggers.Add(new DailyTrigger());
// Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction("notepad.exe", "D:\\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");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想每2分钟运行一次任务.在我的代码中需要更新什么?帮我
我刚刚遇到了同样的挑战.基本上你创建一个TimeTrigger并像这样设置间隔:
// Get the service on the local machine
using (var ts = new TaskService())
{
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.Settings.MultipleInstances = TaskInstancesPolicy.IgnoreNew;
td.RegistrationInfo.Description = "FTP, Photo and Cleanup tasks";
// Create a trigger that will execute very 2 minutes.
var trigger = new TimeTrigger();
trigger.Repetition.Interval = TimeSpan.FromMinutes(2);
td.Triggers.Add(trigger);
// Create an action that will launch my jobs whenever the trigger fires
td.Actions.Add(new ExecAction(System.Reflection.Assembly.GetExecutingAssembly().Location, null, Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)));
// Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition(@"My Task Name", td);
}
Run Code Online (Sandbox Code Playgroud)