运行 TaskService C# 中的方法?

Run Method in TaskService C# ?

我只想在给定日期执行一次函数,我已经完成了 Task。 我该怎么办?

static void Main(string[] args)
{
    DateTime date = GetExecuteTime();        
    using (TaskService ts = new TaskService())
    {
        // Create a new task definition and assign properties
        TaskDefinition td = ts.NewTask();

        td.RegistrationInfo.Description = "run method";

        TimeTrigger d = new TimeTrigger();
        d.StartBoundary = date;

        td.Triggers.Add(d);

        //td.Actions.Add();
    }

    Console.ReadKey(true);
}

创建控制台应用程序,将其放置在您机器上的某个位置,并在任务计划程序中创建事件并在您想要 运行 时添加触发器。

你可以从另一个应用程序设置触发器,你可以在这里找到答案:

Creating Scheduled Tasks

注意,如果不从 Task Scheduler 运行 整合整个应用程序,您就无法 运行 应用程序中的某些随机功能。