C# 如何在taskScheduler中调用方法?
我正在使用任务调度器在一段时间间隔后运行我的应用程序。代码如下所示, 但我想在应用程序启动后运行/调用方法C# 如何在taskScheduler中调用方法?,c#,.net,winforms,C#,.net,Winforms,我正在使用任务调度器在一段时间间隔后运行我的应用程序。代码如下所示, 但我想在应用程序启动后运行/调用方法 using (TaskService ts = new TaskService()) { // Create a new task definition and assign properties TaskDefinition td = ts.NewTask(); td.RegistrationInfo
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);
}
`
在应用程序启动后,我需要在代码中更改什么以运行/调用方法。提前感谢。以下代码使用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;
}
}
我们无法在应用程序启动后调用任何事件或方法,因为此代码可以正常运行以自动启动应用程序,但我想在应用程序启动后执行一些自动工作,因此我想调用特定的方法。如果需要帮助,您必须提供更多信息。如果你启动了“any.exe”,为什么不在“any.exe”中启动你想要的任何应用程序呢?