How to Run a Windows Application as Background Service Without User Login (Windows 7)


36 views

When developing monitoring tools or daemon processes on Windows 7, a persistent challenge emerges: maintaining continuous execution regardless of user login state. Traditional startup methods fail when no interactive session exists.

The Windows Service architecture provides the most robust solution. Services run within their own session (Session 0 isolation) and aren't tied to user sessions. Here's a C# service template:

using System.ServiceProcess;

public class MonitorService : ServiceBase
{
    protected override void OnStart(string[] args)
    {
        // Initialize monitoring logic here
        base.OnStart(args);
    }

    protected override void OnStop()
    {
        // Cleanup resources
        base.OnStop();
    }

    public static void Main()
    {
        ServiceBase.Run(new MonitorService());
    }
}

After compiling, install using sc.exe:

sc create "MonitorService" binPath= "C:\path\to\service.exe" start= auto obj= ".\Administrator" password= "p@ssw0rd"

Key parameters:

  • start= auto for automatic startup
  • obj= specifies the privileged account

Group Policy logon scripts (User Configuration > Policies > Windows Settings > Scripts) only execute during interactive logon events. They won't trigger during:

  • System startup without login
  • Fast user switching
  • Remote desktop disconnections

For non-service executables, create a scheduled task with these settings:

schtasks /create /tn "MonitorTask" /tr "C:\monitor.exe" /sc ONSTART /ru SYSTEM

Advantages:

  • Runs at system startup
  • No password storage required
  • Works with standard executables

Windows 7 enforces strict session isolation. For UI elements:

  • Use WTSSendMessage() for basic notifications
  • Implement a separate GUI controller that communicates with the service
  • Log to Event Log instead of console output

When testing services:

sc start MonitorService
sc queryex MonitorService
eventvwr.msc // View service logs

When implementing 24/7 monitoring solutions on Windows systems, developers often face the critical requirement of maintaining process execution regardless of user login state. Windows 7 presents specific challenges in this regard, with two primary approaches emerging as potential solutions.

Creating a Windows Service provides the most robust solution for persistent background execution. Services operate within the session 0 isolation layer, completely independent of user sessions.

using System.ServiceProcess;

public class MonitorService : ServiceBase
{
    private MonitoringTool _tool;
    
    protected override void OnStart(string[] args)
    {
        _tool = new MonitoringTool();
        _tool.Start();
    }
    
    protected override void OnStop()
    {
        _tool.Stop();
    }
    
    public static void Main()
    {
        ServiceBase.Run(new MonitorService());
    }
}

Key advantages of the service approach:

  • Automatic startup before user login (when configured as Automatic)
  • Continued operation during user logoff/reboot cycles
  • Proper process management through SCM (Service Control Manager)

While group policy logon scripts won't work for this scenario (they require user login), Windows Task Scheduler offers a viable alternative when services aren't feasible:

schtasks /create /tn "MonitorApp" /tr "C:\Monitoring\app.exe" /sc onstart /ru "SYSTEM" /rl HIGHEST

Important configuration parameters:

  • Run as SYSTEM or admin account (/ru parameter)
  • Trigger on system startup (/sc onstart)
  • Run whether user is logged on or not (configure in GUI)

Session 0 Isolation (Windows 7): Services can't interact with the desktop directly. For monitoring hardware or network, use proper APIs rather than GUI-dependent methods.

User Context Matters: Services running as Local System have different privileges than user-account services. Test permission requirements thoroughly.

Startup Dependencies: For monitoring network resources, configure service dependencies to start after network connectivity is established.

When testing service behavior without user login:

sc start MonitorService
eventvwr.msc (check Application/System logs)

For scheduled tasks, verify execution through:

schtasks /query /tn "MonitorApp" /v
%windir%\Tasks (legacy location)