Starting a scheduled task in C#

I had to start a scheduled task. Most of the examples on MSDN use C++ for this, because there are no managed APIs to work with the task scheduler. Lucky for me the COM components for the Task Scheduler are pretty easy to use.

The steps:

  1. Add a COM reference for the TaskScheduler component.
  2. Add   using TaskScheduler;   to your source.
  3. Use the following code as an example:

TaskScheduler.TaskScheduler ts = new TaskScheduler.TaskSchedulerClass();
ts.Connect("faq-xps", "username", "domain", "password");
ITaskFolder tf = ts.GetFolder(@"\");
IRegisteredTask task = tf.GetTask("Test");   // “Test” is the name of the task in the Scheduler
task.Run(null);              // If your task requires parameters, then pass them instead of null

BTW, if you want to do this using a command line utility, you may want to try “schtasks” in a command prompt.

About Gaston

MCT, MCSD, MCDBA, MCSE, MS Specialist
This entry was posted in Development. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s