Vishvadeep singh
Vishvadeep singh

Reputation: 1663

How to pass argument while calling Task Scheduler from c#

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 !

Upvotes: 0

Views: 6170

Answers (2)

Rohith
Rohith

Reputation: 5677

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);


  }

Upvotes: 0

Tejinder Singh
Tejinder Singh

Reputation: 1120

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

Upvotes: 2

Related Questions