RunningTask Parameters/Arguments

Feb 14, 2012 at 7:04 PM

Hi,

I have an existing task pointing to a batch file with no arguments.  I want to pass arguments dynamically to the task when calling <Task>.Run() without updating the actual task and calling RegisterChanges().

string[] sParams = new string[1] { "1" };
RunningTask taskRun = task.Run(sParams);

This code doesn't pass "1" to the task/batch file.  What is the string array used for in the Run function?

Matt

Coordinator
Feb 15, 2012 at 3:08 PM

See this article for more info, but essentially you need to specify "$(Arg0)" as the value for the ExecAction.Arguments property.

Dec 30, 2012 at 11:58 PM

Hello dahall,

I read up on the article you provided but it doesn't tell much. Perhaps you have an example?

I am trying something like: regTask.Run( new KeyValuePair<object, string>("ExecAction.Arguments", $(Arg0)));

 

But it doesn't work. Do you have a sample syntax of what exactly needs to go into the .Run parameter?

 

Charles

Coordinator
Jan 1, 2013 at 1:20 AM

When you create the ExecAction, you need to specify $(Arg0) for the Arguments value.

td.Actions.Add(new ExecAction("myprogram.exe", "$(Arg0)"));

Then, you can call the Run method using the string value you wish to pass as a parameter.

task.Run("My parameter string to pass to myprogram.exe");
Jan 3, 2013 at 8:28 PM
dahall wrote:

When you create the ExecAction, you need to specify $(Arg0) for the Arguments value.

td.Actions.Add(new ExecAction("myprogram.exe", "$(Arg0)"));

Then, you can call the Run method using the string value you wish to pass as a parameter.

task.Run("My parameter string to pass to myprogram.exe");

Hi Dashall,

Sorry for bothering again but when I did:

td.Actions.Add(new ExecAction("C:\test.bat", "$(Arg0)",null));
                    task.RootFolder.RegisterTaskDefinition(ScheduledTaskName, td);

tasks.Run("hello");
It is giving the xml containing too many nodes error. Any ideas?

Jan 3, 2013 at 9:05 PM

Sorry for the earlier confusion. I was using

TaskService task = new TaskService(MachineName,null,null,null,false)

 

instead of

TaskService task = new TaskService(MachineName,null,null,null,true)

 

Now everything works!

 

Thanks for the suggestion!

Coordinator
Jan 3, 2013 at 11:40 PM

You will only get that error with the "false" last parameter to TaskService if you are adding more than one Action to the TaskDefinition. Make sure there is only one and the parameters to TaskService can be set anyway you want. The example I gave above works for V1 and V2 instances of the Task Scheduler.

Jan 4, 2013 at 3:37 PM

I am using V2 to call V1 tasks on another machine so I think it's forcing me to use the force 1.0 compatibility as the last parameter.

It seems like the only way to do it is to re-register the task...do you know if I can bypass the RegisterTaskDefinition step?

Coordinator
Jan 4, 2013 at 4:49 PM

You are correct on both accounts. To call the Run method on a V1 task, you need to force a V1 connection. You always have to call RegisterTaskDefinition after any changes to a task.

Jan 18, 2013 at 11:43 AM
Edited Jan 18, 2013 at 11:45 AM

How to add multiple arguments

if i add

td.Actions.Add(new ExecAction("myprogram.exe", "$(Arg0,Arg1,Arg2)"));
i am getting error
can any one explain about how to pass arguments to myprogram.exe using task schedular
Coordinator
Jan 18, 2013 at 2:31 PM

Try to use the following code and then use separate string items in the Run command.

td.Actions.Add(new ExecAction("myprogram.exe", "$(Arg0),$(Arg1),$(Arg2)"));

task.Run("A", "B", "C");
Jan 21, 2013 at 9:45 AM
Edited Jan 21, 2013 at 9:46 AM

Thanks for reply.

sorry i am not using task.Run() method to pass multiple arguments we need to pass arguments by providing spaces .

 

new ExecAction( "program.exe", id+ " " + startDate + " " + endDate + " " + time, null

 

));

 so that exe will take this as arguments based on the spaces provided

taskDefinition.Actions.Add(

Coordinator
Jan 21, 2013 at 3:57 PM

In order to pass variables that change each time the task runs on its own, you have to use the run command as shown in previous posts. If you just want to pass the variables when the task is registered, you can do exactly as you have posted.