(Print this page)

Interfacing the Windows Task Scheduler
Published date: Sunday, August 1, 2004
On: Moer and Éric Moreau's web site

Ever need to schedule tasks from your own application? Information on that topic is not easily found. Nothing in pure VB.Net allows you to this. But someone out there figured out how to do it and created a wrapper class to address this task. This wrapper can be freely used in your own applications.

This guy is Eduardo A. Morcillo. He is a VB MVP. Notice that I got his express permission to reference his wrapper to write this column.

The wrapper classes for the Windows Task Scheduler

Eduardo’s classes can be used to easily add, modify and delete tasks in the Task Scheduler from .Net applications. Just need to reference the wrapper and you are ready. The wrapper classes even come with a test class that I will use here and extend with a feature I found handy (because all other needs are already implemented in Eduardo’s test project.

You can download it by clicking the Wrapper Classes for the Windows Task Scheduler link from Eduardo’s VB.Net web page at http://www.mvps.org/emorcillo/en/code/shell/index.shtml. For those of you who want to know how he succeeded, full source is included.

You will also find a download link at the end of this article. My link only includes the modified test project and the Edanmo.TaskScheduler assembly.

Basic features

When you start the test project, the list of scheduled tasks will appear in the list at the top left of the form. When you select one these task, its properties appear at the bottom of the form.

Figure 1: The Test application

The “Add New” button and the “Properties” button launch Windows Scheduler dialog to which you are already used. There is a small glitch with the “Add New” button: the task will never start! It seems that the wrapper forgot one property. The newly added task can be run without problem but will not start when scheduled. Watch Eduardo’s website as he may post a fix for that.

Meanwhile, I found a work around to that. If you specify flags (and an end date) when you add a new task, the task get scheduled correctly:

' Add a m_TaskScheduler
With .Triggers.Add()
    .TriggerType = Trigger.Types.Daily
    .BeginDay = Date.Today
    .StartTime = Now
    .Flags = Trigger.TriggerFlags.HasEndDate
    .EndDay = Date.Parse("9999/12/31")
End With

The “Delete” button simply deletes the selected task after confirmation.

The “Run Now!” button starts the job at the time you press the button.

The “Refresh” button reset the list of tasks in case you added/deleted tasks from the Scheduled task applet.

My personal touch

One feature I found to be missing from the test application and that I personally needed to document relief plan at client site is an export facility.

It isn’t very difficult. Everything is already in place. We simply need to loop through the tasks and output the properties into a file. Here I create a .CSV file but it could easily be any other format you need.

Private Sub btnExportList_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                        Handles btnExportList.Click
    Dim objSW As StreamWriter = New StreamWriter("c:\temp\ScheduledTask.csv")
    Dim strTaskDetail As New System.Text.StringBuilder
    Dim strTaskName As String
    Dim strTrigger As New System.Text.StringBuilder

    '--Create a header into the file
    strTaskDetail.Append(Chr(34) & "Task name" & Chr(34) & ",")
    strTaskDetail.Append(Chr(34) & "Application Name" & Chr(34) & ",")
    strTaskDetail.Append(Chr(34) & "Command Line" & Chr(34) & ",")
    strTaskDetail.Append(Chr(34) & "Working Directory" & Chr(34) & ",")
    strTaskDetail.Append(Chr(34) & "Comment" & Chr(34) & ",")
    strTaskDetail.Append(Chr(34) & "Flags" & Chr(34) & ",")
    strTaskDetail.Append(Chr(34) & "Last Run Time" & Chr(34) & ",")
    strTaskDetail.Append(Chr(34) & "Next Run Time" & Chr(34) & ",")
    strTaskDetail.Append(Chr(34) & "Creator" & Chr(34) & ",")
    strTaskDetail.Append(Chr(34) & "Exit Code" & Chr(34) & ",")
    strTaskDetail.Append(Chr(34) & "File Name" & Chr(34) & ",")
    strTaskDetail.Append(Chr(34) & "Max Run Time" & Chr(34) & ",")
    strTaskDetail.Append(Chr(34) & "Status" & Chr(34) & ",")
    strTaskDetail.Append(Chr(34) & "Triggers" & Chr(34))
    objSW.WriteLine(strTaskDetail)

    For Each strTaskName In m_TaskScheduler

        Dim objTask As Edanmo.TaskScheduler.Task
        Dim objTrigger As Edanmo.TaskScheduler.Trigger

        objTask = m_TaskScheduler.Item(strTaskName)

        strTaskDetail.Length = 0

        strTaskDetail.Append(Chr(34) & objTask.Name & Chr(34) & ",")
        strTaskDetail.Append(Chr(34) & objTask.ApplicationName & Chr(34) & ",")
        strTaskDetail.Append(Chr(34) & objTask.CommandLine & Chr(34) & ",")
        strTaskDetail.Append(Chr(34) & objTask.WorkingDirectory & Chr(34) & ",")
        strTaskDetail.Append(Chr(34) & objTask.Comment & Chr(34) & ",")
        strTaskDetail.Append(Chr(34) & Flags(objTask.Flags) & Chr(34) & ",")
        strTaskDetail.Append(Chr(34) & objTask.LastRunTime.ToString("F") & Chr(34) & ",")
        strTaskDetail.Append(Chr(34) & objTask.NextRunTime.ToString("F") & Chr(34) & ",")
        strTaskDetail.Append(Chr(34) & objTask.Creator & Chr(34) & ",")
        strTaskDetail.Append(Chr(34) & objTask.ExitCode & Chr(34) & ",")
        strTaskDetail.Append(Chr(34) & objTask.FileName & Chr(34) & ",")
        strTaskDetail.Append(Chr(34) & objTask.MaxRunTime & Chr(34) & ",")
        strTaskDetail.Append(Chr(34) & objTask.State & Chr(34) & ",")

        strTrigger.Length = 0
        For Each objTrigger In objTask.Triggers
            strTrigger.Append(objTrigger.Text & Environment.NewLine)
        Next
        strTaskDetail.Append(Chr(34) & strTrigger.ToString & Chr(34))

        objTask.Dispose()

        objSW.WriteLine(strTaskDetail)
    Next

    objSW.Close()

    MessageBox.Show("File created.")

End Sub

Conclusion

So if you need to implement task scheduling into your own application, do not look further. Just use it!

Many thanks to Eduardo A. Morcillo for letting me share his wonderful wrapper for the Windows task scheduler.

I hope you appreciated the topic and see you next month.


(Print this page)