0

I have a .NET application running, and I want it to run a task that I already scheduled with the Windows Task Scheduler. The Task is written as its own console application.

I tried listing the arguments in the function: Run(arg1, arg2), but upon reading the documentation I learned this is not correct.

The Task would be deleting certain types of records (defined as an enum)

ASP.NET App:

using (var ts = new TaskService())
{
    var deleteRecords = ts.FindTask("DeleteRecords");
    deleteRecords.Run(); //Want to Pass Args into this task
}

Task

namespace DeleteRecords {
    class Program {
        static void Main(string[] args) {
             string recordName = args[0];
             string recordType = args[1];
             //Then get and Delete Delete Record
        }
    }
}

Right now when I the ASP.NET application, it spawns the task, but exits with an out of index error, which makes sense since the args are not defined.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
ExaExcellion
  • 35
  • 2
  • 11

1 Answers1

0

I've done a lot of research and You can't schedule a task in the task manager then run it dynamically w/ parameters. Any parameters you run it with are part of the definition of the scheduled task, which makes sense.

That being said, there is a way around this: In your code, you can on-the-fly schedule a new task and register it w/ your parameters, which is addressed in @Mikaal Anwar's link above, but if you need to call the task w/ some other parameters, you'll need to make another identical task, but w/ different parameters. This can be problematic because say if you're trying to run the scheduled task as a different user than the one calling the task, you'll need to expose that user's info when registering the task (among other things). However, if those types of things aren't an issue, then this is a perfectly serviceable solution.

Alternatively, in my case I settled on calling my secondary Console App as a subprocess. It has other issues that a scheduled task doesn't have, but it allows me to pass in new parameters each time.

ExaExcellion
  • 35
  • 2
  • 11