enable/disable task

Coordinator
Jul 12, 2011 at 4:20 PM

User savor wrote the following:

I'm trying to enable/disable scheduled task through ASP application like this:

TaskEditDialog editorForm = new TaskEditDialog(ts.RootFolder.Tasks[taskName], true, true);
editorForm.Task.Enabled = true;

but it doesn't enable the task; am I missing something? 


Coordinator
Jul 12, 2011 at 4:25 PM

For an ASP application, you cannot use the Windows Forms available in the TaskSchedulerEditor assembly (like TaskEditDialog). You should use the TaskScheduler assembly by itself.

To enable a task you only need a few lines of code:

Task t = ts.GetTask(taskName);
if (t != null)
{
  t.Enabled = true;
  t.RegisterChanges();
}
Jul 12, 2011 at 5:10 PM

Thanks i just tried that however  on the line

 

t.RegisterChanges(); I get following exception:

Trigger.Repetition.Interval must be less than Trigger.Repetition.Duration under Task Scheduler 1.0.

 

here is what i'm doing:

TaskService ts  = new TaskService(ConfigurationManager.AppSettings["server1"],
                                     ConfigurationManager.AppSettings["userName1"],
                                     ConfigurationManager.AppSettings["domain"],
                                     ConfigurationManager.AppSettings["password1"], false);

 Microsoft.Win32.TaskScheduler.Task t = (Microsoft.Win32.TaskScheduler.Task)ts.GetTask(taskName);

      if (t != null)
            {
                t.Enabled = false;
                t.RegisterChanges();
            }
     
       
   

Coordinator
Jul 13, 2011 at 6:20 PM

I believe you are using an older version of the library that had a bug. Please make sure you are using the 1.6.2 release.


Jul 15, 2011 at 2:52 AM

I try to disable an existing schedule in Task Scheduler using these lines of code:

using (TaskService ts = new TaskService())
                {
                    Task task = ts.GetTask("TaskName");
                    task.Enabled = false;
                    task.RegisterChanges();
                }

However, it givers an error at line task.RegisterChanges().

COM exception was handled.

The task has been configured with an unsupported combination of account settings and run time options. (Exception from HRESULT: 0x80041314).

This scheduled runs under my user account.

Can you give me some ideas?

Coordinator
Jul 15, 2011 at 8:27 PM

What version of Windows are you using? If Vista or later, will you export the XML for the task and post it so I can see the properties you have set that may be causing the error?

Jul 16, 2011 at 9:28 AM

Hi,

I use Windows XP. It can work when I run these lines of code in Windows 7, but cannot in Windows XP with td.Principal.LogonType = TaskLogonType.InteractiveToken;

By the way, in Windows 7, I can not set td.Settings.RunOnlyIfLoggedOn = false; with any td.Principal.LogonType. Can you give me how to create a task in Windows 7 ( or all version of Windows) that can run when users log off, because I need the task can run when they log off.

 

Thanks

Coordinator
Jul 18, 2011 at 7:43 PM

You are correct, TaskLogonType.InteractiveToken only works on Vista and later. TaskDefinition.Settings.RunOnlyIfLoggedOn is only available if you force version 1 or are on XP and earlier.

However, I know of no way to trigger a task when the user logs off using the Task Scheduler.