简体   繁体   中英

Run Method in TaskService C# ?

I want to execute a function in given date just once and i'm done with the 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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