This project has moved. For the latest updates, please go here.

How to schedule task at specific time

Mar 13, 2012 at 11:17 PM

I can't find an example of how to create a trigger for a specific time.  For example I would like to start a trigger using military time.  Any help would be appreciated.

Coordinator
Mar 13, 2012 at 11:42 PM
using (TaskService ts = new TaskService())
{
   ts.AddTask("TaskName",
      new TimeTrigger { StartBoundary = new DateTime(2012, 5, 1, 23, 30, 00) },
      new ExecAction("notepad.exe", null, null));
}

This code will run Notepad at 23:30 on May 1, 2012. Check the examples pages under the Documentation tab for many more examples.

Mar 20, 2012 at 8:32 PM

I've been combing through the examples but still struggling with what i need to do.  I have been able to create a task at a specific time but now i'm stuck trying to set that task to run daily.  My code below is creating 2 triggers one for the time and one to run daily I need to configure a single trigger that will schedule a task to run at a specific time each day.  Any help would be appreciated.

        public string CreateTask(string server, string ScriptName, string taskname, string desc, string param, int hour, int min, int sec)
        {
            string msg = "SUCCESS";
            // Get the service on the local machine
            using (TaskService ts = new TaskService(server))
            {

                // Display version and server state
                Version ver = ts.HighestSupportedVersion;
                bool newVer = (ver >= new Version(1, 2));
                string connected = ts.Connected ? "Connected" : "Disconnected";

                //for testing only
                msg = msg + " Scheduler Highest version: " + ver + "Scheduler Level:" + newVer + " Server: " + ts.TargetServer + " " + connected;

                // Create a new task definition and assign properties
                TaskDefinition td = ts.NewTask();
                td.RegistrationInfo.Description = desc;
                td.Principal.LogonType = TaskLogonType.InteractiveToken;

                // Create an action that will launch a script
                td.Actions.Add(new ExecAction(ScriptName, param, null));

                // Create an ation that will launch at a certain time
                TimeTrigger tt = (TimeTrigger)td.Triggers.Add(new TimeTrigger { StartBoundary = DateTime.Today + TimeSpan.FromHours(hour) + TimeSpan.FromMinutes(min) + TimeSpan.FromSeconds(sec)});

                // Add a trigger that will fire the task at this time every day
                DailyTrigger dt = (DailyTrigger)td.Triggers.Add(new DailyTrigger { DaysInterval = 1 });
               
                // Register the task in the root folder (this shoiuld be in a try / catch logic
                try
                {
                    ts.RootFolder.RegisterTaskDefinition(@taskname, td);
                }
                catch (Exception ex)
                {
                    msg = ex.Message.ToString();
                    msg = msg.ToLower();
                }

            }
            return msg;
        }

Mar 20, 2012 at 8:43 PM
schall123 wrote:
Apparently i was overthinking what i needed to do.  I had to remove the timetrigger and add the time information into the dailytrigger.
                // Add a trigger that will fire the task at this time every day
                DailyTrigger dt = (DailyTrigger)td.Triggers.Add(new DailyTrigger
                                                                    { DaysInterval = 1 ,
                                                                      StartBoundary = DateTime.Today + TimeSpan.FromHours(hour) + TimeSpan.FromMinutes(min) + TimeSpan.FromSeconds(sec) });

I've been combing through the examples but still struggling with what i need to do.  I have been able to create a task at a specific time but now i'm stuck trying to set that task to run daily.  My code below is creating 2 triggers one for the time and one to run daily I need to configure a single trigger that will schedule a task to run at a specific time each day.  Any help would be appreciated.

        public string CreateTask(string server, string ScriptName, string taskname, string desc, string param, int hour, int min, int sec)
        {
            string msg = "SUCCESS";
            // Get the service on the local machine
            using (TaskService ts = new TaskService(server))
            {

                // Display version and server state
                Version ver = ts.HighestSupportedVersion;
                bool newVer = (ver >= new Version(1, 2));
                string connected = ts.Connected ? "Connected" : "Disconnected";

                //for testing only
                msg = msg + " Scheduler Highest version: " + ver + "Scheduler Level:" + newVer + " Server: " + ts.TargetServer + " " + connected;

                // Create a new task definition and assign properties
                TaskDefinition td = ts.NewTask();
                td.RegistrationInfo.Description = desc;
                td.Principal.LogonType = TaskLogonType.InteractiveToken;

                // Create an action that will launch a script
                td.Actions.Add(new ExecAction(ScriptName, param, null));

                // Create an ation that will launch at a certain time
                TimeTrigger tt = (TimeTrigger)td.Triggers.Add(new TimeTrigger { StartBoundary = DateTime.Today + TimeSpan.FromHours(hour) + TimeSpan.FromMinutes(min) + TimeSpan.FromSeconds(sec)});

                // Add a trigger that will fire the task at this time every day
                DailyTrigger dt = (DailyTrigger)td.Triggers.Add(new DailyTrigger { DaysInterval = 1 });
               
                // Register the task in the root folder (this shoiuld be in a try / catch logic
                try
                {
                    ts.RootFolder.RegisterTaskDefinition(@taskname, td);
                }
                catch (Exception ex)
                {
                    msg = ex.Message.ToString();
                    msg = msg.ToLower();
                }

            }
            return msg;
        }

 

Coordinator
Mar 21, 2012 at 12:04 AM

DaysInterval is set to 1 by default. You can rewrite that line as:

td.Triggers.Add(new DailyTrigger { StartBoundary = DateTime.Today + new TimeSpan(hour, min, sec) });