1

我想创建每 2 分钟触发一次的任务计划程序。我正在使用以下命名空间

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

我想每 2 分钟运行一次任务。在我的代码中需要更新什么?帮我

4

5 回答 5

9

我刚刚遇到了同样的挑战。基本上,您创建一个 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);
    }
于 2013-03-10T13:40:07.410 回答
2

由于您使用的是Task Scheduler Managed Wrapper库,我建议您查阅Triggers的文档。更具体地说,阅读如何使用TimeTrigger类以及如何使用它来指定重复间隔的示例。

于 2012-08-02T02:47:15.227 回答
2

我不知道代码,但是......你需要指定频率。在命令行上运行:

schtasks /create /SC MINUTE /MO 2 /TN DoThis /tr "notepad d:\test.log"

这应该每 2 分钟重复一次(在 cmd 行上)。

于 2012-08-01T13:13:55.237 回答
1

如果您想在特定时间触发为什么不使用服务,它将每 2 分钟自动触发您的电脑启动,例如

Timer timer = new Timer();

protected override void OnStart(string[] args)
    {

        //handle Elapsed event
        timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);

        //This statement is used to set interval to 2minute (= 60,000 milliseconds)

        timer.Interval = 120000;

        //enabling the timer
        timer.Enabled = true;


    }
 private void OnElapsedTime(object source, ElapsedEventArgs e)
    {
       // writr code here for 
      //run your Note pad file using process.start or using batch file
    }
于 2013-11-23T10:43:25.933 回答
0

为 Task Scheduler v2.0 提供更新的答案。

您不能再以小于 5 分钟的间隔重复任务,但您始终可以使用 StartBoundary 偏移数分钟多次注册任务,以获得您需要的确切重复模式。如果您决定这样做,请务必更改 regTask 参数中的名称,否则注册只会更新先前注册的任务。

为下面的额外使用引用道歉,我从一个更大的模块中提取了这个,不想花时间弄清楚下面的代码不需要哪些:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using TaskScheduler;

private void CreateWindowsTask()
{
    TaskScheduler.TaskScheduler ts = new TaskScheduler.TaskScheduler();
    ts.Connect(null, null, null, null); //connect to local machine as current user
    ITaskDefinition task = ts.NewTask(0);
    task.RegistrationInfo.Author = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    task.RegistrationInfo.Description = "Put your desription here";

    ITrigger trigger = (ITrigger)task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_DAILY);
    trigger.Id = "MyTimeTrigger";
    string StartTime = "T" + DateTime.Now.ToString("HH:MM:00");
    trigger.StartBoundary = DateTime.Now.ToString("yyyy-MM-dd") + StartTime;
    trigger.EndBoundary = DateTime.Now.Date.AddYears(2).ToString("yyyy-MM-dd") + "T12:00:00";
    trigger.Repetition.Interval = "PT5M";   //5 minutes
    trigger.Repetition.Duration = "P1D";    //repeat for 1 day
    trigger.Repetition.StopAtDurationEnd = true;

    IExecAction action = (IExecAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
    action.Id = "MyAction";
    action.Path = "C:\\Windows\\System32\\notepad.exe";

    ITaskFolder root = ts.GetFolder("\\");
    IRegisteredTask regTask = root.RegisterTaskDefinition(
        "Task Scheduler Demo",
        task,
        (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE,
        null, null, //user, passwrod
        _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN,
        ""          //sddl
        );
    IRunningTask runTask = regTask.Run(null);
}
于 2019-11-06T20:25:31.657 回答