0

Here I have create service for writing .txt file and passing parameter from Task scheduler using this code :

static void Main(string[] args)
    {

        string abc = string.Empty;
        foreach (var item in args)
        {
            abc += item +" ";
        }
        string path = @"D:\GST Project\Demo Text File.txt";
        File.WriteAllText(path, abc);
    }

I have added the task in task scheduler like this: Task scheduler

I want to call my scheduler task by using C# code below is my code which I have taken from Link

using (TaskService tasksrvc = new TaskService(server.Name, login, domain, password))
{
    Task task = tasksrvc.FindTask(taskName);
    task.Run();       
}

I am wondering here how we can pass the parameter through TaskService. Also What should I pass in place of Server.Name,login, domain,Password. Thanks for your help !

Vishvadeep singh
  • 1,624
  • 1
  • 19
  • 31
  • Is the argument the same on every run? Or does it vary from run to run? – mjwills Jul 26 '17 at 10:49
  • Why call the scheduler task like that? Why not just invoke the console application directly? _Is it because it is running remotely?_ – mjwills Jul 26 '17 at 10:50
  • @mjwills I need to perform every action through scheduler not write code to call exe directly, Also I run it from c# application so I will pass parameter from c# code. Also it is running remotely – Vishvadeep singh Jul 26 '17 at 10:51
  • `I need to perform every action through scheduler not write code to call exe directly` Can you explain why that constraint exists (in your post)? Why do you need to do it that way? – mjwills Jul 26 '17 at 10:55
  • @mjwills in scheduler I can handle the stop and start but through calling exe I need to kill the process I have not another way to stop it. – Vishvadeep singh Jul 26 '17 at 11:01

2 Answers2

2

try the below code:- This will help you

TaskService.Instance.AddTask("Test", QuickTriggerType.Daily, "Exe file path", "test");

this will schedule your task and pass the test argument in your exe file

Tejinder Singh
  • 1,070
  • 2
  • 8
  • 24
0

What should I pass in place of Server.Name,login, domain,Password

serverName - The name of the computer that you want to connect to. If the serverName parameter is empty, then this method will execute on the local computer.

login-The user name that is used during the connection to the computer. If the user is not specified, then the current token is used.

domain - The domain of the user specified in the user parameter.

password-The password that is used to connect to the computer. If the user name and password are not specified, then the current token is used

.

You can use a TaskDefinition to pass parmeters and other settings. Please try the following for passing the parameters

using (TaskService ts = new TaskService()) {

     TaskDefinition td = ts.NewTask();
     td.RegistrationInfo.Description = "Does something";

     //l fire the task at this time every day
     td.Triggers.Add(new DailyTrigger { DaysInterval = 1 });

     // Create an action that will launch Notepad and you can pass paremeters
     td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));

     // Register the task in the root folder
     ts.RootFolder.RegisterTaskDefinition(@"Test", td);


  }
Rohith
  • 5,527
  • 3
  • 27
  • 31