0

我正在使用任务调度程序在一段时间后运行我的应用程序。代码如下所示,但我想在我的应用程序启动后运行/调用一个方法

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);


        }

` 应用程序启动后,我需要更改代码以运行/调用方法。提前致谢。

4

1 回答 1

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;
            }
        }
于 2013-07-16T13:29:38.113 回答