Task Sheduler Microsoft.Win32.TaskScheduler

Topics: Examples
Nov 20, 2016 at 1:45 PM
Edited Nov 20, 2016 at 1:46 PM

I want to create a task scheduler on weekly basis. i want my exe file run on Monday & Tuesday around 11:00 AM in morning.

How to do the code
below is code
Private Sub TaskRunSheduler()
        Using 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.LogonType = TaskLogonType.InteractiveToken

            ' Add a trigger that will fire the task at this time every other day
            Dim dt As DailyTrigger = DirectCast(td.Triggers.Add(New DailyTrigger(2)), DailyTrigger)
            dt.Repetition.Duration = TimeSpan.FromHours(11)
            dt.Repetition.Interval = TimeSpan.FromHours(0)

            ' Add a trigger that will fire every week on Friday

            ' below i will put my exe file
            td.Actions.Add(New ExecAction("D:\Atn.exe"))

            ' Register the task in the root folder
            Const taskName As String = "RunWeeklyBasis"
            ts.RootFolder.RegisterTaskDefinition(taskName, td)
        End Using

    End Sub
Dec 6, 2016 at 8:39 PM
One line of code!
Private Sub TaskRunSheduler()
  ' Create a new task definition and assign properties
  Dim t As Task = TaskService.Instance.AddTask("RunWeeklyBasis",
    New WeeklyTrigger With { .StartBoundary = DateTime.Today + TimeSpan.FromHours(11), .DaysOfWeek = DaysOfTheWeek.Monday Or DaysOfTheWeek.Tuesday },
    New ExecAction("D:\Atn.exe"))
End Sub
Marked as answer by dahall on 12/6/2016 at 1:39 PM