2019/05/13

C#.Net Win32.TaskScheduler 2小時執行一次

執行結果:








using System;
using Microsoft.Win32.TaskScheduler;


namespace CYFang
{
    public partial class Form1: Form 
    {
        public Form1() 
        {
            using (var taskService = TaskService.Instance)
            {
                using (var task = taskService.NewTask())
                {
                    task.Triggers.Add(new DailyTrigger
                    {
                        StartBoundary = DateTime.Now,
                        Repetition = new RepetitionPattern(TimeSpan.FromHours(2), TimeSpan.FromHours(2))
                    });
                    //run program
                    task.Actions.Add(@"C:\test.exe");
                    //Description
                    task.RegistrationInfo.Description = "說明";
                    //Author
                    task.RegistrationInfo.Author = "C.Y. Fang";
                    //Add task to Task Service
                    taskService.RootFolder.RegisterTaskDefinition("名稱", task);
                }
            }
        }
    }
}

參考資料:
https://github.com/dahall/taskscheduler
https://stackoverflow.com/questions/21939133/create-repetitive-task-using-task-scheduler

沒有留言:

張貼留言