Quantcast
Channel: Task Scheduler Managed Wrapper
Viewing all articles
Browse latest Browse all 2206

New Post: Trying to disable a task (vb)

$
0
0
Hi, I create a task using the following code...


Try
        Dim ts As New TaskService()

        ' Create a new task definition and assign properties
        Dim td As TaskDefinition = ts.NewTask()
        td.RegistrationInfo.Description = "Does something"
        td.Principal.RunLevel = TaskRunLevel.Highest
        td.Principal.LogonType = TaskLogonType.InteractiveToken

        ' Create a trigger that will fire the task at 06:00 every day.
        Dim dt As New DailyTrigger(1)
        dt.StartBoundary = CDate(DateAdd(DateInterval.Day, 1, Now()).ToString("dd/MM/yyyy 06:00:00"))
        td.Triggers.Add(dt)

        'create a trigger that will fire the task every thursday at 04:00.
        Dim wt As WeeklyTrigger = DirectCast(td.Triggers.Add(New WeeklyTrigger()), WeeklyTrigger)
        wt.StartBoundary = CDate(DateAdd(DateInterval.Day, 1, Now()).ToString("dd/MM/yyyy 04:00:00"))
        wt.DaysOfWeek = DaysOfTheWeek.Thursday

        ' Create an action that will launch Notepad whenever the trigger fires
        td.Actions.Add(New ExecAction("notepad.exe", "c:\test.txt", Nothing))

        ' Register the task in the root folder
        ts.RootFolder.RegisterTaskDefinition("TestTask", td)

    Catch ex As Exception
        MsgBox("error - " & ex.Message)
    End Try

...and I'm trying to disable it using the following
    Try
        Dim ts As New TaskService

        Dim t As Task = ts.GetTask("TestTask")
        t.Enabled = False
        t.RegisterChanges()
    Catch ex As Exception
        MsgBox("error - " & ex.Message)
    End Try

...but when I use the code to disable it, the code runs OK, but the task isn't disabled.

What am I missing?

PS - the code to define the triggers looks a bit messy as I'm still getting my head around this - sorry.

Viewing all articles
Browse latest Browse all 2206

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>