Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheduled Task using Task Scheduler Managed Wrapper

I have done a lot of searching over the last few days in regards to Checking if a scheduled task exists, if so <insert awesome here>. Basically I have an app that installs and uninstalls our software's scheduled tasks. What I need to do now is have a checkbox be checked if the task is there and unchecked if it's not. There was a reference to using:

        ScheduledTasks st = new ScheduledTasks(server);
        string[] taskNames = st.GetTaskNames();
        List<string> jobs = new List<string>(taskNames);

which isn't working for me, it's stating that the ScheduledTasks namespace could not be found. I believe I have what I need installed. "using Microsoft.Win32.TaskScheduler;"

like image 227
ShaneTheTech Avatar asked Mar 24 '23 07:03

ShaneTheTech


2 Answers

I haven't seen this ScheduledTasks within this wrapper.

The TaskScheduler Managed Wrapper uses a service idiom and you need to have the context of a folder.

They have good examples in their documentation, including one for enumerating all tasks.

If you want to find a particular task:

var t = taskService.GetTask(scheduledTaskName); 
bool taskExists = t!=null;
if(taskExists) DoYourThing();

if your tasks are within a folder, then use something like the following

var t = taskService.GetTask(taskFolder + "\\" + scheduledTaskName);
like image 104
dove Avatar answered Apr 16 '23 13:04

dove


To add to the answer by Dove (I don't have the rep to comment yet), you can also use the FindTask method for TaskScheduler, which allows you to neglect specifying the sub-folder with the name if you enable searching within sub-folders by passing "True" as the second parameter; for instance.

using ( TaskService sched = new TaskService() ) {
    var task = sched.FindTask( "UniqueTaskName", true );

    if ( task != null ) {
        ...
    }
}
like image 32
Noob-o-tron 5000 Avatar answered Apr 16 '23 12:04

Noob-o-tron 5000