task launches as scheduled, but doesn't graphically show Notepad

Topics: Errors, Examples
Feb 25, 2015 at 2:52 PM
Edited Feb 25, 2015 at 2:54 PM
I have some code which I use to schedule a task to launch notepad.exe after 2 minutes have elapsed.

The process seems to start fine, but the program doesn't show up in the UI.

Please help.

Here is the code:
        Dim ts As New TaskService
        Dim td As TaskDefinition = ts.NewTask

        Dim tf As TaskFolder = ts.RootFolder

        td.RegistrationInfo.Description = "Test action"
        td.Principal.LogonType = TaskLogonType.S4U
        td.Settings.DisallowStartIfOnBatteries = False
        td.Settings.Hidden = False
        td.Settings.Priority = System.Diagnostics.ProcessPriorityClass.Normal
        td.Settings.RunOnlyIfIdle = False
        td.Settings.RunOnlyIfNetworkAvailable = False
        td.Settings.StopIfGoingOnBatteries = False

        td.Principal.RunLevel = TaskRunLevel.Highest
        td.Settings.AllowDemandStart = True
        td.Settings.AllowHardTerminate = True
        td.Settings.Compatibility = TaskCompatibility.V2
        td.Settings.DeleteExpiredTaskAfter = TimeSpan.FromMinutes(1)
        td.Settings.StartWhenAvailable = True

        Dim tt As New TimeTrigger


        tt.StartBoundary = DateTime.Now + TimeSpan.FromMinutes(2)
        tt.EndBoundary = DateTime.Now + TimeSpan.FromMinutes(2)

        tt.Repetition.StopAtDurationEnd = True

        tt.Enabled = True

        td.Actions.Add(New ExecAction("notepad.exe", "C:\test.txt"))


        tf.RegisterTaskDefinition("TestNewest", td, TaskCreation.CreateOrUpdate, My.User.Name, , TaskLogonType.S4U)

        Catch ex As Exception
        End Try

        Debug.Print("Task registered, will run at " + CStr(DateTime.Now + TimeSpan.FromMinutes(2)))
Mar 2, 2015 at 9:39 PM
To show an application running interactively, you must specify a LogonType of InteractiveToken. You can skip setting the td.Principal.LogonType property and just do it through the RegisterTaskDefinition method (e.g. RegisterTaskDefinition("TestNewest", td, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken)).
Marked as answer by dahall on 3/2/2015 at 2:40 PM