每2分钟运行一次任务计划程序
问题描述:
我想创建每2分钟触发一次的任务计划程序。 我使用以下使用Microsoft.Win32.TaskScheduler每2分钟运行一次任务计划程序
我已经写了下面的代码
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分钟运行任务namesapce
。需要什么来更新我的代码? 帮我
答
我不知道在代码中,但是......你需要指定频率。在命令行上运行此:
的schtasks /创建/ SC MINUTE/MO 2/TN DoThis/TR “记事本d:\ test.log中”
这应该重复每2分钟(对cmd线)。
答
由于您使用的是Task Scheduler Managed Wrapper库,所以建议您参考Triggers的文档。更具体地说,请阅读如何使用TimeTrigger
类的示例以及如何使用它来指定重复间隔。
答
我刚刚面临同样的挑战。 基本上你创建一个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);
}
答
如果要触发特定的时间你为什么不使用的服务, 将触发每2分钟自动将您的PC启动, 例如
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
}
如果你要在命令行中运行这个...: 的schtasks /创建/ SC分/ MO 2/TN DoThis/TR “记事本d:\ test.log中” – 2012-08-01 13:17:39
大概你可以设置SC并在对象属性的某个地方使用MO。 :) – 2012-08-01 13:18:39
如果不是...... exec shell命令? – 2012-08-01 13:28:38