This project has moved and is read-only. For the latest updates, please go here.

Get trigger properties from an existing task

Sep 18, 2009 at 12:40 AM

I need to get trigger properties (daily? weekly? startbounding? repetition? etc, etc) from an existing task.

Actually I do this:

TaskFolder mailBrowserBackupTasksFolder = backupTaskService.GetFolder(@"\");
TaskCollection listOfTasks = mailBrowserBackupTasksFolder.Tasks;

foreach (Task theTask in listOfTasks)
{
     if (theTask.Name.Contains(titleOfTask))
     {
          nextBackupOn = theTask.NextRunTime.ToString();
          disallowStartIfOnBatteriesFlag = theTask.Definition.Settings.DisallowStartIfOnBatteries;
     }

 

I tried this statement in the "if" cycle:

MessageBox.Show(theTask.Definition.Triggers.GetEnumerator().Current.ToString());

 

but I got the NullReferenceException. Why? How can I obtain the trigger properties of an existing task?

Many thanks!

Regards,

Alessandro.

Sep 18, 2009 at 8:12 AM

Well. I partially resolved.

I used this code:

TaskFolder mailBrowserBackupTasksFolder = backupTaskService.GetFolder(@"\");
TaskCollection listOfTasks = mailBrowserBackupTasksFolder.Tasks;
IEnumerator<Trigger> pippo;

 foreach (Task theTask in listOfTasks)
{

if (theTask.Name.Contains(titleOfTask))
{

pippo=theTask.Definition.Triggers.GetEnumerator();
pippo.MoveNext();
MessageBox.Show(pippo.Current.ToString());
nextBackupOn = theTask.NextRunTime.ToString();
disallowStartIfOnBatteriesFlag = theTask.Definition.Settings.DisallowStartIfOnBatteries;

}              

}   

 

Coordinator
Sep 21, 2009 at 6:22 AM

Here's how I would do it:

using (TaskService ts = new TaskService())
{
   Task theTask = ts.GetTask("titleOfTask");
   if (theTask != null && theTask.Definition.Triggers.Count > 0)
      MessageBox.Show(theTask.Definition.Triggers[0].ToString());
}
Sep 21, 2009 at 11:47 AM

I tried to use the GetTask method but I got error with it.

Now with your code runs very well. Thank you so much!