2

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.

Aditya Korti
  • 692
  • 2
  • 12
  • 28

1 Answers1

2

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.

m1o2
  • 1,549
  • 2
  • 17
  • 27
  • "Connects to a remote machine and associates all subsequent calls on this interface with a remote session." But I am not connecting to a remote machine – Aditya Korti Mar 15 '16 at 07:06
  • @AdityaKorti what is your error message? What is not working? Also, please see this example: http://stackoverflow.com/questions/7394806/creating-scheduled-tasks – m1o2 Mar 15 '16 at 07:09
  • It does not give any error message.It just does not do anything, And I had seen that question.But I don't know why it tells to delete the task in the end. – Aditya Korti Mar 15 '16 at 07:13
  • @AdityaKorti I don't have access right now to a dev machine. I'll test and fix it when I'll get home at the evening. – m1o2 Mar 15 '16 at 11:43
  • Okay it runs. The problem was that I didn't trigger it. – Aditya Korti Mar 15 '16 at 11:47
  • Great, please submit a new answer with what you did :) – m1o2 Mar 15 '16 at 12:13