我使用 taskscheduler 在一段时间间隔后运行我的应用程序。代码如下所示, 但我希望在应用程序启动后运行/调用方法。
using (TaskService ts = new TaskService())
{
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Open App";
// Create a trigger that will fire the task now
Trigger tg = Trigger.CreateTrigger(TaskTriggerType.Time);
Trigger tri = Trigger.CreateTrigger(TaskTriggerType.Event);
tg.StartBoundary = DateTime.Now.AddMinutes(1);
td.Triggers.Add(tg);
// Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction(@"any.exe", null, null));
// Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition(@"Test", td);
}
` 在应用程序启动后我需要在代码中更改运行/调用方法。提前谢谢。
答案 0 :(得分:0)
以下代码使用XML来加载任务,但它可以为您提供注册任务调度程序的缺失代码:
using (TaskService ts = new TaskService())
{
var folder = ts.RootFolder.SubFolders.FirstOrDefault(folderItem => folderItem.Name
== folderName);
//folder doesn't exist, we will create it
if (folder == null)
{
folder = ts.RootFolder.CreateFolder(folderName);
}
string xmlTaskData = string.Empty;
if (File.Exists(fileLocation))
{
xmlTaskData = File.ReadAllText(fileLocation);
}
else
{
return false;
}
var task = folder.Tasks.FirstOrDefault(taskInFolder => taskInFolder.Name == taskName);
//doesn't exist, we will add it using default xml
if (task == null)
{
task = CreateTask(ts, folder, taskName, xmlTaskData);
}
if (task != null)
{
// enable/disable the task
task.Enabled = add;
task.Definition.Settings.Enabled = add;
task.RegisterChanges();
return true;
}
else
{
return false;
}
}