简体   繁体   中英

TaskService not working

I'm trying to write some code that uns the task on my local workstation after a certain period of time but at the moment I'm having problems getting the work to be done.

Below is the code I am running.

using System;
using Microsoft.Win32.TaskScheduler;

namespace TaskSchedularExamplw
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Task Started");
            using (TaskService ts = new TaskService())
            {
                TaskDefinition t = ts.NewTask();
                t.Triggers.Add(new TimeTrigger() { StartBoundary = DateTime.Now, Enabled = true });
                t.Principal.LogonType = TaskLogonType.InteractiveToken;
                TimeTrigger tt = (TimeTrigger)t.Triggers.Add(new TimeTrigger() { StartBoundary = DateTime.Now, Enabled = true });
                tt.Repetition.Duration = TimeSpan.FromHours(1);
                tt.Repetition.Interval = TimeSpan.FromMinutes(1);
                t.Actions.Add(new ExecAction(@"C:\Program Files (x86)\Notepad++\notepad++.exe", "c:\\test.txt", null));
                const string taskName = "Test";
                ts.RootFolder.RegisterTaskDefinition(taskName, t);
                var runningTasks=ts.GetRunningTasks();
            }

            Console.ReadLine();
        }
    }
}

Can someone please tell me what exactly is I am doing wrong here.

According to the TaskService MSDN page , you should call the Connect method before invoking any other of the TaskService methods.

The TaskService.Connect method should be called before calling any of the other TaskService methods.

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