This project has moved. For the latest updates, please go here.

using C# how do you get a list of current Scheduled Tasks?

Jul 8, 2010 at 9:11 PM

Good Day:

I've been trying to figure out a way of being able to bring back data on Scheduled Tasks.

Basically I want to get the collection of Tasks, Loop thru them and display data from that.

I've tried the following but I can't get it to work.

Microsoft.Win32.TaskScheduler.

TaskCollection taskNames = default(Microsoft.Win32.TaskScheduler.TaskCollection);

 

string sTaskList = "<table>";
<font size="2">

 

</font>foreach (Microsoft.Win32.TaskScheduler.Task t in taskNames)

{

 

<font size="2">

 

</font>string sApplicationName = t.Name;

//The original version of this task scheduler had a way to get the Creator, but I don't see that with this version.

// string sCreator = ""; //t.Creator;  <font size="2">

 

</font>string sNextRungTime = t.NextRunTime.ToString();<font size="2"> </font><font size="2">

 

</font>
<font size="2">sTaskList = sTaskList +</font>
<font size="2">

 

</font>"<tr><td class='normaltablecell'>" + sApplicationName + "</td><td class='normaltablecell'>" + sCreator + "</td><td class='normaltablecell'>" + sNextRungTime + "</td></tr>";

}

<font size="2">

sTaskList = sTaskList +

</font>

 

"</table>";

 

I try and use this code and it doesn't work.

Any ideas on the best way to be able to bring this data back using this Wrapper?

Thanks for your help.

Mary

Coordinator
Jul 12, 2010 at 9:47 PM

There are a few considerations:

  1. Which OS will the applications be running under? If Vista/Svr03 or later, then tasks are in a hierarchal structure. Prior to that, there is a single folder.
  2. Do you want running tasks or all tasks?
  3. The data for each task is found either at the Task object level (for more run-time info) or under the TaskDefinition's properties.
  4. What information do you want about the actions? Not all tasks trigger an application. Some can trigger messages, emails, and COM execution.

A list of all tasks in the root folder along with basic information can be retrieved using the following code:

TaskService ts = new TaskService();
foreach (Task t in ts.RootFolder.Tasks)
{
   try
   {
      Console.WriteLine("+ {0}, {1} ({2})", t.Name, t.Definition.RegistrationInfo.Author, t.NextRunTime);
      foreach (Trigger trg in t.Definition.Triggers)
         Console.WriteLine(" + {0}", trg);
      foreach (var act in t.Definition.Actions)
         Console.WriteLine(" = {0}", act);
   }
   catch { }
}