C# TaskScheduler

Discussion in 'Mixed Languages' started by mephistooo2, Jun 9, 2013.

  1. mephistooo2

    mephistooo2 MDL Novice

    Feb 5, 2008
    33
    27
    0

    I need help for my project

    How to create multiple triggers taskscheduler with C# code?

    Thanks for helps :rolleyes:
     
  2. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,110
    120
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,812
    45,370
    150
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,323
    60
    Unless you spark your own tasks with WinAPI - P/Invoke

    I know a number of system tools can do this, you just need your app to record dates, times and run at startup.
     
  5. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,812
    45,370
    150
    #5 CODYQX4, Jun 10, 2013
    Last edited by a moderator: Apr 20, 2017
    This is what I use in MTK:
    Task Scheduler Managed Wrapper

    Some code from how I create AutoKMS task:
    Code:
            /// <summary>
            /// Create the AutoKMS Scheduled Task
            /// </summary>
            public static void MakeAutoKMSNormalTask()
            {
                DailyTrigger dt = new DailyTrigger();
                BootTrigger bt = new BootTrigger();
                LogonTrigger lt = new LogonTrigger();
                List<Trigger> triggerList = new List<Trigger> {dt, bt, lt};
    
                Tasks.CreateTask(AutoKMSInstallPath, AutoKMSFileName, AutoKMSProcessName, null, triggerList);
            }
    
    Code:
            /// <summary>
            /// Create a new Scheduled Task
            /// </summary>
            /// <param name="installPath">Directory where the program to schedule resides</param>
            /// <param name="fileName">Name of the program file to schedule</param>
            /// <param name="taskName">Name of the Scheduled Task to create</param>
            /// <param name="taskFolder">Create Task in a Folder other than the Root Folder</param>
            /// <param name="triggers">List of Triggers to activate the task. If null, will add a Daily Trigger </param>
            public static void CreateTask(string installPath, string fileName, string taskName, string taskFolder = null, IEnumerable<Trigger> triggers = null)
            {
                using (TaskService ts = new TaskService())
                {
                    // Create a new task definition and assign properties
                    TaskDefinition td = ts.NewTask();
                    td.RegistrationInfo.Description = taskName;
    
                    // Allow Run On Battery Power
                    td.Settings.DisallowStartIfOnBatteries = false;
                    td.Settings.StopIfGoingOnBatteries = false;
    
                    // Set Privilege Level
                    try
                    {
                        td.Principal.RunLevel = TaskRunLevel.Highest;
                    }
                    catch (NotV1SupportedException)
                    {
                        //Ignore, not supported on XP
                    }
    
                    // Create an action that will launch whenever the trigger fires
                    td.Actions.Add(new ExecAction(installPath + "\\" + fileName));
    
                    // Add Triggers
                    if (triggers != null)
                    {
                        foreach (Trigger trigger in triggers)
                        {
                            td.Triggers.Add(trigger);
                        }
                    }
                    else
                    {
                        // Add a Daily Trigger
                        DailyTrigger dt = new DailyTrigger();
                        td.Triggers.Add(dt);
                    }
                    
                    // Register the task in the passed folder
                    if (taskFolder != null)
                    {
                        // This will throw a NotV1SupportedException on Windows XP or Older
                        ts.GetFolder(taskFolder).RegisterTaskDefinition(taskName, td, TaskCreation.CreateOrUpdate, "SYSTEM", null, TaskLogonType.ServiceAccount);
                    }
                    // Register the task in the root folder
                    else
                    {
                        ts.RootFolder.RegisterTaskDefinition(taskName, td, TaskCreation.CreateOrUpdate, "SYSTEM", null, TaskLogonType.ServiceAccount);
                    }
                }
            }
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...