Share via

scheduler tasks with security options “Run whether user is logged on or not”.

YOFFE Mike 40 Reputation points
2026-02-26T14:32:57.9+00:00

We use the package <package id="TaskScheduler" version="2.12.2" targetFramework="net48" />

This is the code with three actions and its behavior:

|| | -------- | Task

|| | -------- | GUI

|| | -------- | Manually run from the scheduler

|| | -------- | Automatically run from the scheduler according to the trigger

Task

|| || GUI

|| || Manually run from the scheduler

|| || Automatically run from the scheduler according to the trigger

|| || Cmd echo

|| || No

|| || Run as expected

|| || Run as expected

|| || Notepad

|| || Yes

|| || Run in the background and does not work and not ended

|| || Run as in the trigger time in the background and does not work and not ended

|| || Our utility

|| || No

 

The same applications work fine if we use the option “Run only when user is logged on”

= = = = = = = = = = = = = = 

using (TaskService ts = new TaskService())

 {

     TaskDefinition td = ts.NewTask();

     td.RegistrationInfo.Description = "Run task as SYSTEM user";

 

     // ✅ Run whether user is logged on or not

     td.Principal.UserId = "SYSTEM";

     td.Principal.LogonType = TaskLogonType.ServiceAccount;

     td.Principal.RunLevel = TaskRunLevel.Highest;

 

     // Trigger: run once, 1 minute from now

     td.Triggers.Add(new TimeTrigger

     {

         StartBoundary = DateTime.Now.AddMinutes(1)

     });

 

     // Action

1.       td.Actions.Add(new ExecAction(@"C:\Windows\System32\cmd.exe /c ", 

"echo SYSTEM task ran >> C:\temp\system_task.log", null));

2.       td.Actions.Add("C:\Windows\notepad.exe");

3.       td.Actions.Add(new ExecAction(@"OurCustom.exe",”Our arguments”));

 

     // Register task

     ts.RootFolder.RegisterTaskDefinition( "Test", td, TaskCreation.CreateOrUpdate, null, null,

 TaskLogonType.ServiceAccount);

 

     //Console.WriteLine("Task created and runs as SYSTEM.");

 }

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments
{count} votes

Answer accepted by question author
  1. RLWA32 52,256 Reputation points
    2026-03-05T11:27:52.6633333+00:00

    @YOFFE Mike I broke out a very old virtual machine that had VB6 installed. I copied the VB6 console code from Creating a real console application in Visual Basic as well as the code to change the SUBSYSTEM.

    I removed all the code from the console application project and replaced it with the code to instantiate and use the scripting FileSystem object.

    Built the project and changed the resultant executable SUBSYSTEM . Then I copied the VB6 console application to a Win10 22H2 system and ran it as a scheduled task under SYSTEM account as described for the previous proof of concept.

    The VB6 console application completed successfully;y and created the test file in the C:\Users\Public\intergraph folder.

    I could not replicate your issue.

    0 comments No comments

Answer accepted by question author
  1. Q&A Assist
    2026-02-26T14:33:12.9733333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    The behavior is by design for tasks configured to “Run whether user is logged on or not” under the SYSTEM (service) account.

    Key points:

    1. Desktop interaction is not available
      • When a task runs as SYSTEM with TaskLogonType.ServiceAccount and “Run whether user is logged on or not”, it runs in a non-interactive session.
      • GUI applications such as Notepad (or any app that expects a user desktop) cannot display a window or interact with the logged-on user. They appear to “run in the background” and typically hang or never complete properly.
      • This is why:
        • cmd /c echo ... works: it is a non-interactive console command that does not require a desktop.
        • notepad.exe and the custom utility (if it expects UI or interactive desktop resources) do not work and appear stuck.
    2. Why it works when “Run only when user is logged on”
      • In this mode, the task runs in the interactive user session with access to the user’s desktop.
      • GUI apps (Notepad, custom UI tools) can create windows and behave as expected.
    3. Recommended patterns
      • For background/system tasks:
        • Use console or service-style executables that do not depend on UI or desktop interaction.
        • Keep actions like cmd.exe /c ... or non-UI utilities.
      • For tasks that must show UI:
        • Configure the task to “Run only when user is logged on” and run under that user’s account, not SYSTEM.
      • Avoid running GUI apps under SYSTEM with TaskLogonType.ServiceAccount because they cannot reliably interact with any user session.
    4. Task Scheduler model
      • Task Scheduler tasks are defined by triggers and actions. Actions that execute command-line operations (like ExecAction with cmd.exe) are supported for non-interactive scenarios.
      • GUI actions are not suitable for non-interactive service accounts.

    To fix the current code:

    • Keep the cmd.exe action as-is for background logging.
    • Remove or replace the Notepad and any UI-dependent actions when using SYSTEM + “Run whether user is logged on or not”.
    • If Notepad or the custom utility must be visible, create a separate task configured as “Run only when user is logged on” under a real user account.

    References:


6 additional answers

Sort by: Most helpful
  1. YOFFE Mike 40 Reputation points
    2026-03-05T07:59:52.95+00:00

    Yes, I can make changes. It is not enough to write as in my code to "c:\users\public\intergraph\test.txt"?

    0 comments No comments

  2. YOFFE Mike 40 Reputation points
    2026-03-04T08:57:47.64+00:00

    Sub Main()

    Dim fso As New Scripting.FileSystemObject 

    Dim ts As Scripting.TextStream

    fso.CreateTextFile ("c:\users\public\intergraph\test.txt") 

    Set ts = fso.OpenTextFile("c:\users\public\intergraph\test.txt", ForWriting) 

    ts.WriteLine ("Test text") 

    ts.Close 

    Exit Sub

    End Sub

    I build the standard EXE

    I created two scheduled tasks that execute this code.
    The SINGLE difference is:

    • Run only if user is logged on
    • Run whether user is logged on or not

    In the first case it runs, in the second it is stalled and does not do anything. We have customers that say that our scheduled application worked for years and now stopped to work.

     


  3. YOFFE Mike 40 Reputation points
    2026-03-01T15:38:25.5066667+00:00

    I wrote the minimum code

    Sub Main()

    Dim fso As New Scripting.FileSystemObject
    
    Dim yourStream As Scripting.TextStream
    
    
    
    fso.CreateTextFile ("aaa.log")
    
    Set yourStream = fso.OpenTextFile("c:\users\public\intergraph\aaa.log")
    
    yourStream.WriteLine ("Enter")
    
    yourStream.Close
    

    End Sub

    I run it manually from the scheduler. It runs forever, I see it in the Task Manager, but no write is executed. If I put the mode "Run only if the user is logged on" it works


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.