简体   繁体   English

如何在taskScheduler中调用方法?

[英]How to call a method in a taskScheduler?

I am using a taskscheduler to run my application after some time interval. 我正在使用任务计划程序在一段时间后运行我的应用程序。 The code is shown below, but i want to run/call a method after my application has started. 代码如下所示,但是我想在应用程序启动后运行/调用方法

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


        }

` what i need to change in my code to run/call method after application has started. 在应用程序启动后,我需要在代码中更改哪些内容才能运行/调用方法。 Thanks in advance. 提前致谢。

The following code uses XML to load the task, but it can give you the missing code for registering for the task scheduler: 以下代码使用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;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM