Introduction
Whenever we schedule a task in Windows we always use itaskscheduler or taskScheduler as we can get its DLL or library very easily in our
Windows system in C:\Windows\System32\ drive as taskschd.dll. But this time it is something different.
The library of the task scheduler was used till Vista [Windows Taskscheduler 1.0]
now when we work in upgraded versions of windows a new wrapper class has been launched by
Windows as itaskservice which helps programmers to work with the Windows Taskscheduler 2.0.
This wrapper class needs an external DLL called Microsoft.Win32.TaskScheduler.
Background
I developed this code in Visual studio 2012 so you will need the same. You can download
the .dll from the following link also:
As a reference of this task you can take a reference of codeplex.com's link:
http://taskscheduler.codeplex.com/documentation.
Using the code
Imports Microsoft.Win32.TaskScheduler
Imports System.IO
Public Class Form1
Public Shared Function ScheduleTaskInWin_TaskScheduler(
ByRef sunday As String, _
ByRef monday As String, _
ByRef tues As String, _
ByRef wedn As String, _
ByRef thursday As String, _
ByRef friday As String, _
ByRef saturday As String
)
Using ts As New TaskService()
Dim td As TaskDefinition = ts.NewTask
td.RegistrationInfo.Description = _
"Any description which you want to give for you task. "
Dim wt As New WeeklyTrigger()
wt.StartBoundary = DateTime.Today.AddDays(0)
wt.DaysOfWeek = sunday Or monday Or tues Or wedn Or thursday Or friday Or saturday
wt.Repetition.Duration = TimeSpan.FromHours(24)
wt.Repetition.Interval = TimeSpan.FromDays(1)
td.Triggers.Add(wt)
Dim FolderPath As String = AppDomain.CurrentDomain.BaseDirectory
Dim ExeLocation As String = Path.Combine(FolderPath, "nameOfExe.exe")
ExeLocation = ExeLocation.Replace("\"c, "/"c)
td.Actions.Add(New ExecAction(ExeLocation, "c:\nameOfLogFile.log"))
ts.RootFolder.RegisterTaskDefinition("NameOfScheduleTask", td)
End Using
Return ""
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim sunday As String = DaysOfTheWeek.Sunday
Dim monday As String = DaysOfTheWeek.Monday
Dim tuesday As String = DaysOfTheWeek.Tuesday
Dim Wednesday As String = DaysOfTheWeek.Wednesday
Dim Thursday As String = DaysOfTheWeek.Thursday
Dim Friday As String = DaysOfTheWeek.Friday
Dim Saturday As String = DaysOfTheWeek.Saturday
ScheduleTaskInWin_TaskScheduler(sunday, monday, _
tuesday, Wednesday, Thursday, Friday, Saturday)
End Sub
End Class
Points of Interest
You can see you created task in same C:\windows\Tasks location and your scheduled task in task scheduler.