Use C # to create a scheduled Task (How to create a Task scheduse use C #)
Requirement: multiple background programs (winfrom, wpf, console, and so on) are run to update data in an indefinite period of time. Q: Why do I need to use a scheduled task instead of a timer in the program? A: The most obvious thing is that the Timer Program is always running in the background, but it only needs to run once a day or once a month. It's not a waste of CPU resources to keep running timing in the background. Solution: 1. manually create a scheduled task in the control panel using the built-in scheduled tasks in windows. 2. use the built-in class library TaskScheduler ("c: \ windows \ system32 \ taskachd. dll ") to create 3. use Process. the Star () doscommand is used to create a scheduled task. In this example, the TaskScheduler class library comes with Microsoft. The following is the encapsulated code, it includes deleting a scheduled task, determining whether a scheduled task exists, obtaining all scheduled tasks, and creating scheduled tasks. (For details, see the Notes ): public class SchTaskExt {// <summary> /// delete task // </summary> /// <param name = "taskName"> </param> private static void deleteTask (string taskName) {TaskSchedulerClass ts = new TaskSchedulerClass (); ts. connect (null, null); ITaskFolder folder = ts. getFolder ("\"); folder. deleteTask (taskName, 0);} // <summary> // get all tasks /// </summary> public static IRegistered TaskCollection GetAllTasks () {TaskSchedulerClass ts = new TaskSchedulerClass (); ts. connect (null, null); ITaskFolder folder = ts. getFolder ("\"); IRegisteredTaskCollection tasks_exists = folder. getTasks (1); return tasks_exists ;} /// <summary> /// check task isexists // </summary> /// <param name = "taskName"> </param> /// <returns> </returns> public static bool IsExists (string taskName) {Var isExists = false; IRegisteredTaskCollection tasks_exists = GetAllTasks (); for (int I = 1; I <= tasks_exists.Count; I ++) {IRegisteredTask t = tasks_exists [I]; if (t. name. equals (taskName) {isExists = true; break ;}} return isExists ;} /// <summary> /// create task /// </summary> /// <param name = "creator"> </param> /// <param name = "taskName"> </param> // <param name = "path"> </param> // <param Name = "interval"> </param> // <returns> state </returns> public static _ TASK_STATE CreateTaskScheduler (string creator, string taskName, string path, string interval) {try {if (IsExists (taskName) {DeleteTask (taskName);} // new scheduler TaskSchedulerClass scheduler = new TaskSchedulerClass (); // pc-name/ip, username, domain, password scheduler. connect (null, null); // get scheduler folder ITa SkFolder folder = scheduler. getFolder ("\"); // set base attr ITaskDefinition task = schedtion. newTask (0); task. registrationInfo. author = "McodsAdmin"; // creator task. registrationInfo. description = "... "; // description // set trigger (IDailyTrigger ITimeTrigger) ITimeTrigger tt = (ITimeTrigger) task. triggers. create (_ TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME); tt. repetition. interval = interval; // format PT1H 1 M = 1 hour the value set in 1 minute will eventually be converted to minutes added to the trigger tt. startBoundary = "2015-04-09T14: 27: 25"; // start time // set action IExecAction action = (IExecAction) task. actions. create (_ TASK_ACTION_TYPE.TASK_ACTION_EXEC); action. path = path; task. settings. executionTimeLimit = "PT0S"; // does the running task time out to stop the task? PTOS does not enable time-out tasks. settings. disallowStartIfOnBatteries = false; // The task is executed only in the AC power supply. settings. runOnlyIfIdle = false; // IRegisteredTask regTask = folder is executed only when the computer is idle. registerTaskDefinition (taskName, task, (int) _ TASK_CREATION.TASK_CREATE, null, // user null, // password _ TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, ""); IRunningTask runTask = regTask. run (null); return runTask. state;} catch (Exception ex) {Throw ex ;}}note: 1. reference taskachd. after dll is selected, press F4 to change the embedded interoperability type to False in the attribute (if not set, an error will be reported: Unable to embed the interoperability type "TaskScheduler. taskSchedulerClass ". Use the applicable interface instead.) 2. schdule. connec ("pc-name or ip", "username", "domain", "password") 3. multiple trigger types are available (daily IDailyTrigger and hourly ITimeTrigger). The format of the trigger frequency (Interval) must follow the format of "PT1H1M; the start time must follow the format "YYYY-MM-DDThh: mm: ss.