2

I'm developing add-on based application. Every add-on use scheduler. Loaded add-on schedule task. My application run only one instance. Sometimes application closed, sometimes running. Therefore i need to use Windows 7 Task scheduler

How to use task scheduler on own application?

I need create new task from application
I need that when task finish, Send message to my application or invoke any function of my application
Is it possible?
How to do it?

ebattulga
  • 10,774
  • 20
  • 78
  • 116

3 Answers3

5

Check out this project at CodeProject.

A New Task Scheduler Class Library for .NET

  • Yes. It works on Vista, so it'll work on Windows 7. It does something along the lines of what Kate Gregory describes. –  Jul 29 '10 at 15:29
5

If you want to interact with the Windows 7 Scheduled Tasks system from your code to create, manage, or delete a task that is no problem. (I cover this in the Windows 7 course I wrote for Pluralsight.) Add a COM reference to TaskScheduler and you then do this sort of thing:

ITaskService scheduler = new TaskSchedulerClass();
scheduler.Connect(null, null, null, null);

ITaskFolder rootFolder = scheduler.GetFolder("\\");
ITaskFolder folder = rootFolder.GetFolders(0).Cast<ITaskFolder>().FirstOrDefault(
    f => f.Name == "Windows7Course");

if (folder == null)
{
    folder = rootFolder.CreateFolder("Windows7Course", null);
}

ITaskDefinition task = scheduler.NewTask(0);
IExecAction action = (IExecAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);

action.Path = typeof(SayHello.Form1).Assembly.Location;
action.WorkingDirectory = Path.GetDirectoryName(typeof(SayHello.Form1).Assembly.Location);

ISessionStateChangeTrigger trigger = (ISessionStateChangeTrigger)task.Triggers.Create(
    _TASK_TRIGGER_TYPE2.TASK_TRIGGER_SESSION_STATE_CHANGE);

trigger.StateChange = _TASK_SESSION_STATE_CHANGE_TYPE.TASK_SESSION_UNLOCK;

task.Settings.DisallowStartIfOnBatteries = true;

task.RegistrationInfo.Author = "Kate Gregory";
task.RegistrationInfo.Description = "Launches a greeting.";

IRegisteredTask ticket = folder.RegisterTaskDefinition(
    "GreetReturningUser", 
    task, 
    (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, 
    null, 
    null, 
    _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, 
    null);

This particular sample runs an exe that is in the same solution (another project). You would have to adjust the Action.Path, Action.WorkingDirectory etc to meet your needs.

davmos
  • 9,324
  • 4
  • 40
  • 43
Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
  • 1
    this is a good start. However, there is a managered wrapper for Task Scheduler in CodePlex.com which makes it much easier. http://taskscheduler.codeplex.com/ – newman Mar 25 '11 at 19:40
0

Thanks for the excellent example Kate, I rue that in my wanderings I didn't come across your code first.

I've had a merry dance with registering scheduled tasks via c# code and unsurprisingly have had to tweak my schedule creation code as I encounter roadblocks on one environment that I hadn't encountered on any previously. I hope I've arrived at the most robust code and thought it might also be of benefit to others if I share some things you might try if encountering environmental issues:

  • Put the path to the executable in double quotes
  • Do not put the working folder in quotes
  • Once the task is created, if you make subsequent changes, call the ask.RegisterChanges(); - it may be worth setting the task.Definition.Principal.RunLevel = TaskRunLevel.Highest;
  • I typically default to a System account to run the task, but in some network environments I've encountered issues which I was unable to fathom but was able to resolve by providing a user account for the task to run under.

all the best Matt

mattpm
  • 1,310
  • 2
  • 19
  • 26