Get Delay from Trigger?

Feb 15, 2015 at 4:28 PM
Edited Feb 15, 2015 at 4:32 PM
How do I get the delay time from a trigger?

For instance...
Dim delayTime as short
Using taskServiceObject As TaskService = New TaskService()
  Dim taskObject As Microsoft.Win32.TaskScheduler.Task = taskServiceObject.GetTask("mytask")

  If (taskObject Is Nothing) = False Then ' Makes sure that the task exists and we don't get a Null Reference Exception.
    With taskObject.Definition.Triggers.Item(0)
      If .TriggerType = TaskTriggerType.Logon Then
        // Get Delay Time Here and store it in a variable
        delayTime = .Delay????
      End If
    End With
  End If
End Using
Anybody have a clue how to get that?
Coordinator
Feb 17, 2015 at 3:44 PM
First of all, tasks do not have to have any triggers so unless you know there is at least one trigger, you should check Triggers.Count > 0 before getting the indexed trigger.

To answer your question, I believe you are looking for the following:
Dim delayTime As TimeSpan
Using taskServiceObject As TaskService = New TaskService()
   Dim taskObject As Microsoft.Win32.TaskScheduler.Task = taskServiceObject.GetTask("mytask")
   ' Makes sure that the task exists and we don't get a Null Reference Exception.
   If (taskObject IsNot Nothing) And (taskObject.Definition.Triggers.Count > 0) Then
      Dim trigger As Trigger = taskObject.Definition.Triggers.Item(0)
      If (trigger.TriggerType = TaskTriggerType.Logon) And (TypeOf trigger Is ITriggerDelay) Then
         ' Get Delay Time Here and store it in a variable
         delayTime = CType(trigger, ITriggerDelay).Delay
      End If
   End If
End Using
Technically, you don't need to check if the trigger type is Logon unless your code requires it. If you need seconds or milliseconds from the TimeSpan object, there are methods and properties on that object to extract that details.
Marked as answer by dahall on 2/17/2015 at 8:44 AM