Introduction
A brief example of how to create a simple task in Windows Task Scheduler using VB. NET
Background
Tested on Windows 8.
A question in the CodeProject Quick Answers area prompted me to create a test program that shows how to create a task in Task Scheduler.
Specifically, the behavior of the Hidden
attribute was in question. I used this code to determine the cause of the issue. See the comments in the code below to learn about the issue with the Hidden
attribute.
Using the code
Copy the source code and modify to your needs. If you need C#, use one of the free code translators to translate it for you.
Add a Reference to C:\WINDOWS\SYSTEM32\taskschd.dll or C:\WINDOWS\SYSWOW64\taskschd.dll which will create Interop.TaskScheduler.dll.
Imports TaskScheduler
Public Sub CreateSchedule()
Dim taskService As ITaskService = New TaskScheduler.TaskScheduler()
taskService.Connect()
Dim taskDefinition As ITaskDefinition = taskService.NewTask(0)
taskDefinition.Settings.MultipleInstances = _TASK_INSTANCES_POLICY.TASK_INSTANCES_IGNORE_NEW
taskDefinition.RegistrationInfo.Description = "Testing the creation of a scheduled task via VB .NET"
taskDefinition.Principal.LogonType = _TASK_LOGON_TYPE.TASK_LOGON_GROUP
taskDefinition.Settings.Hidden = False
Dim taskTrigger As IDailyTrigger = _
DirectCast(taskDefinition.Triggers.Create _
(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_DAILY), IDailyTrigger)
taskTrigger.StartBoundary = _
DateTime.Now.AddMinutes(10).ToString _
("yyyy-MM-ddThh:mm:ss" & _
TimeZone.CurrentTimeZone.GetUtcOffset(Now).ToString.Substring(0, 6))
taskTrigger.DaysInterval = 2
Dim taskAction As IExecAction = _
DirectCast(taskDefinition.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC), IExecAction)
taskAction.Path = "C:\windows\notepad.exe"
taskAction.Arguments = ""
taskAction.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Const taskName As String = "Testing creation of a scheduled task"
Dim rootFolder As ITaskFolder = taskService.GetFolder("\")
rootFolder.RegisterTaskDefinition( _
taskName, taskDefinition, _
CInt(_TASK_CREATION.TASK_CREATE_OR_UPDATE), _
"MyUsername", "MyPassword", _
_TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN_OR_PASSWORD)
While System.Runtime.InteropServices.Marshal.ReleaseComObject(rootFolder) <> 0
Application.DoEvents()
End While
rootFolder = Nothing
While System.Runtime.InteropServices.Marshal.ReleaseComObject(taskTrigger) <> 0
Application.DoEvents()
End While
taskTrigger = Nothing
While System.Runtime.InteropServices.Marshal.ReleaseComObject(taskAction) <> 0
Application.DoEvents()
End While
taskAction = Nothing
While System.Runtime.InteropServices.Marshal.ReleaseComObject(taskDefinition) <> 0
Application.DoEvents()
End While
taskDefinition = Nothing
While System.Runtime.InteropServices.Marshal.ReleaseComObject(taskService) <> 0
Application.DoEvents()
End While
taskService = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
History
- Version 1 - 14 June 2013.
I’m retired. When I started my career, programming projects consisted of plugging wires into plug boards to create punch card processing applications to be run on electrical accounting machine like the IBM 402, 407, 085, 088, 514, 519, etc. From there, I moved to writing SPS and Autocoder applications on an IBM 1401 with 4K of memory eventually upgraded to 16K of memory. After many years of migrating my skills to various languages on various hardware platforms, I became an Information Technology Director where I didn’t need to program anymore. So, starting in 1996, I volunteered my time with a local community cable television organization and built some applications to help them run their operations. Originally in Clipper Summer 1987 and later Clipper 5.2, I migrated and enhanced those applications to VB .NET 2003 in 2003. I retired from my full-time job in 2010. Since then, I have continued to support the local community cable tv organization's applications. In 2013, I migrated the VB .NET 2003 Solution to VB .NET 2012 so that it can run on 64-bit computers and interact with Microsoft Office 2010. The upgrade went smoothly. In mid 2013, I developed a VB .NET 2012 application for them to download election results data from the Secretary of State's web site, format the results and send them to a VizRT character generator for on-air display.