TaskRunEx Method

Runs the registered task immediately using specified flags and a session identifier.

Definition

Namespace: Microsoft.Win32.TaskScheduler
Assembly: Microsoft.Win32.TaskScheduler (in Microsoft.Win32.TaskScheduler.dll) Version: 2.11.1
public RunningTask RunEx(
	TaskRunFlags flags,
	int sessionID,
	string user,
	params string[] parameters
)

Parameters

flags  TaskRunFlags
Defines how the task is run.
sessionID  Int32

The terminal server session in which you want to start the task.

If the UseSessionId value is not passed into the flags parameter, then the value specified in this parameter is ignored.If the UseSessionId value is passed into the flags parameter and the sessionID value is less than or equal to 0, then an invalid argument error will be returned.

If the UseSessionId value is passed into the flags parameter and the sessionID value is a valid session ID greater than 0 and if no value is specified for the user parameter, then the Task Scheduler service will try to start the task interactively as the user who is logged on to the specified session.

If the UseSessionId value is passed into the flags parameter and the sessionID value is a valid session ID greater than 0 and if a user is specified in the user parameter, then the Task Scheduler service will try to start the task interactively as the user who is specified in the user parameter.

user  String
The user for which the task runs.
parameters  String

The parameters used as values in the task actions. A maximum of 32 parameters can be supplied. To run a task with no parameters, call this method without any values (e.g.

C#
RunEx(0, 0, "MyUserName")
).

The string values that you specify are paired with names and stored as name-value pairs. If you specify a single string value, then Arg0 will be the name assigned to the value. The value can be used in the task action where the $(Arg0) variable is used in the action properties.

If you pass in values such as "0", "100", and "250" as an array of string values, then "0" will replace the $(Arg0) variables, "100" will replace the $(Arg1) variables, and "250" will replace the $(Arg2) variables used in the action properties.

For more information and a list of action properties that can use $(Arg0), $(Arg1), ..., $(Arg32) variables in their values, see Task Actions.

Return Value

RunningTask
A RunningTask instance that defines the new instance of the task.

Remarks

This method will return without error, but the task will not run if the AllowDemandStart property of ITaskSettings is set to false for the task.

If RunEx is invoked from a disabled task, it will return null and the task will not be run.

Example

C#
// Run the current task with a parameter as a different user and ignoring any of the conditions.
var runningTask = myTaskInstance.RunEx(TaskRunFlags.IgnoreConstraints, 0, "DOMAIN\\User", "info");
Console.Write(string.Format("Running task's current action is {0}.", runningTask.CurrentAction));

Exceptions

NotV1SupportedExceptionNot supported under Task Scheduler 1.0.

See Also