简体   繁体   English

在TaskService C#中运行方法?

[英]Run Method in TaskService C# ?

I want to execute a function in given date just once and i'm done with the Task . 我只想在给定的日期执行一次功能,而Task已经完成。 What should i do? 我该怎么办?

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

Create console app, place it somewhere on your machine ,and create event in Task Scheduler and add a trigger when u want to run it. 创建控制台应用程序,将其放置在计算机上的某个位置,并在Task Scheduler中创建事件,并在您要运行它时添加触发器。

U can set trigger form another application , you can find answer here: 您可以在另一个应用程序中设置触发器,您可以在此处找到答案:

Creating Scheduled Tasks 创建计划任务

Notice , you can't run some random function from your application without running the whole app from Task Scheduler. 注意,如果不从Task Scheduler运行整个应用程序,则无法从应用程序运行某些随机函数。

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

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