Skip to content

pmonitor0/StreamGuard

Repository files navigation

StreamGuard

A lightweight .NET library that prevents Windows sleep, display power-off and hibernation during long-running tasks such as streaming, monitoring or automation.

.NET Platform License


Features

✔ Prevent Windows sleep ✔ Prevent display power-off ✔ Optional hibernation control ✔ Automatic restore of original power settings ✔ Lightweight .NET library ✔ Works with Console, WPF or background services


Why StreamGuard?

Many users try to prevent Windows sleep using manual power settings or simple utilities.

StreamGuard provides a more reliable solution.

Compared to manual power settings

Manual configuration can easily be forgotten or changed by the system.

StreamGuard:

  • automatically applies the required power policies
  • keeps the system awake while running
  • restores the original settings when finished

Compared to simple "keep awake" tools

Many tools only call SetThreadExecutionState.

StreamGuard also manages:

  • display timeout policy
  • sleep timeout policy
  • optional hibernation control

Designed for developers

StreamGuard is implemented as a reusable .NET library that can be integrated into:

  • Console applications
  • WPF applications
  • monitoring tools
  • background services
  • automation systems

Quick Start

using StreamGuard.Core;

var options = new StreamGuardOptions();

using var service = new StreamGuardService(options);

service.Log += Console.WriteLine;

await service.StartAsync();

// long running work here
await Task.Delay(TimeSpan.FromHours(1));

await service.StopAsync();

Architecture

StreamGuard is built around a small service layer that manages Windows power APIs.

+-----------------------+
| Console / WPF / Host  |
+-----------+-----------+
            |
            v
+-----------------------+
|  StreamGuardService   |
+-----------+-----------+
            |
      +-----+-----+
      |           |
      v           v
+-----------+ +------------------+
| Options   | | Snapshot         |
+-----------+ +------------------+
            |
            v
+-------------------------------+
| Internal Windows integration  |
+---------------+---------------+
                |
         +------+------+
         |             |
         v             v
   PowerPolicyApi   HibernateFacade
         |             |
         v             v
    powrprof.dll   powercfg.exe

Service Lifecycle

Stopped
   |
   v
Starting
   |
   v
Capture snapshot
   |
   v
Apply policy
   |
   v
Running
   |
   v
Maintain loop
   |
   v
Stopping
   |
   v
Restore snapshot
   |
   v
Stopped

The service also exposes events:

service.StateChanged += s => Console.WriteLine($"State: {s}");
service.Log += Console.WriteLine;

Configuration

Behavior can be customized using StreamGuardOptions.

Example:

var options = new StreamGuardOptions
{
    MaintainInterval = TimeSpan.FromSeconds(30),

    PreventDisplayOff = true,
    PreventSleep = true,
    DisableHibernate = true,

    DisplayTimeoutAcSeconds = 0,
    DisplayTimeoutDcSeconds = 0,

    SleepTimeoutAcSeconds = 0,
    SleepTimeoutDcSeconds = 0
};

A value of 0 usually means never.


Project Structure

StreamGuard
 ├─ StreamGuard.Core
 ├─ StreamGuard.Console
 ├─ StreamGuard.Wpf

StreamGuard.Core contains the reusable library.

The Console and WPF projects are simple demo hosts.


Windows Requirements

StreamGuard relies on Windows power APIs.

Recommended environment:

  • Windows 10 or Windows 11
  • .NET 6 or later

Some power policy operations may require administrator privileges.


License

MIT License

About

Prevent Windows sleep, display power-off and hibernation during long-running tasks.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages