Skip to content

Commit de6b958

Browse files
committed
Initial release of StreamGuard
0 parents  commit de6b958

39 files changed

Lines changed: 2184 additions & 0 deletions

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
bin/
2+
obj/
3+
.vs/
4+
*.user
5+
*.suo
6+
*.cache
7+
*.pdb

LICENSE

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
MIT License
2+
3+
Copyright (c) 2026 Ferenc Biro
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software.
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.

README.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# StreamGuard
2+
3+
StreamGuard is a small Windows utility library that helps prevent the system from entering sleep mode, turning off the display, or entering hibernation during long-running tasks such as streaming.
4+
5+
The project provides a reusable .NET library (`StreamGuard.Core`) and optional demo applications (Console and WPF).
6+
7+
---
8+
9+
# Features
10+
11+
StreamGuard can:
12+
13+
* Prevent the display from turning off
14+
* Prevent the system from entering sleep mode
15+
* Disable hibernation while running
16+
* Maintain the keep-awake state using a background watchdog
17+
* Restore the original power settings when stopped
18+
19+
The library works by combining:
20+
21+
* `SetThreadExecutionState` (real-time keep awake)
22+
* Windows Power Policy APIs (`powrprof.dll`)
23+
* `powercfg.exe` where required
24+
25+
---
26+
27+
# Project Structure
28+
29+
```
30+
StreamGuard
31+
├─ StreamGuard.Core # Main reusable library
32+
├─ StreamGuard.Console # Example console host
33+
├─ StreamGuard.Wpf # Example WPF UI
34+
```
35+
36+
`StreamGuard.Core` contains the complete implementation and is the only required component.
37+
38+
---
39+
40+
# Installation
41+
42+
Currently the library can be referenced directly from source.
43+
44+
Later it may be distributed as a NuGet package.
45+
46+
---
47+
48+
# Basic Usage
49+
50+
```csharp
51+
using StreamGuard.Core;
52+
53+
var options = new StreamGuardOptions();
54+
55+
using var service = new StreamGuardService(options);
56+
57+
service.Log += Console.WriteLine;
58+
59+
await service.StartAsync();
60+
61+
// Long running work here
62+
await Task.Delay(TimeSpan.FromHours(2));
63+
64+
await service.StopAsync();
65+
```
66+
67+
---
68+
69+
# Configuration
70+
71+
`StreamGuardOptions` allows control over which policies are enforced.
72+
73+
Example:
74+
75+
```csharp
76+
var options = new StreamGuardOptions
77+
{
78+
MaintainInterval = TimeSpan.FromSeconds(30),
79+
80+
PreventDisplayOff = true,
81+
PreventSleep = true,
82+
DisableHibernate = true,
83+
84+
DisplayTimeoutAcSeconds = 0,
85+
DisplayTimeoutDcSeconds = 0,
86+
87+
SleepTimeoutAcSeconds = 0,
88+
SleepTimeoutDcSeconds = 0
89+
};
90+
```
91+
92+
`0` usually means **never**.
93+
94+
---
95+
96+
# Service Lifecycle
97+
98+
The service exposes the following states:
99+
100+
```
101+
Stopped → Starting → Running → Stopping
102+
103+
Faulted
104+
```
105+
106+
You can subscribe to events:
107+
108+
```csharp
109+
service.StateChanged += s => Console.WriteLine($"State: {s}");
110+
service.Log += Console.WriteLine;
111+
```
112+
113+
---
114+
115+
# Windows Requirements
116+
117+
StreamGuard relies on Windows power APIs.
118+
119+
* Windows 10 / 11 recommended
120+
* Some power policy changes may require administrator privileges
121+
122+
---
123+
124+
# License
125+
126+
MIT (recommended)

StreamGuard.Console/Program.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using StreamGuard.Core;
2+
3+
public class Program
4+
{
5+
[STAThread]
6+
public static async Task Main(string[] Args)
7+
{
8+
9+
var service = new StreamGuardService(new StreamGuardOptions
10+
{
11+
PreventDisplayOff = true,
12+
PreventSleep = true,
13+
DisableHibernate = true,
14+
MaintainInterval = TimeSpan.FromSeconds(30)
15+
});
16+
17+
service.Log += Console.WriteLine;
18+
service.StateChanged += s => Console.WriteLine($"STATE: {s}");
19+
20+
Console.WriteLine("S=start, T=stop, P=state, ESC=exit");
21+
22+
while (true)
23+
{
24+
var k = Console.ReadKey(true);
25+
if (k.Key == ConsoleKey.Escape) break;
26+
27+
switch (k.Key)
28+
{
29+
case ConsoleKey.S: await service.StartAsync(); break;
30+
case ConsoleKey.T: await service.StopAsync(); break;
31+
case ConsoleKey.P: Console.WriteLine(service.State); break;
32+
}
33+
}
34+
35+
await service.StopAsync();
36+
}
37+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net9.0-windows</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<ApplicationManifest>app.manifest</ApplicationManifest>
8+
<StartupObject>Program</StartupObject>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\StreamGuard.Core\StreamGuard.Core.csproj" />
13+
</ItemGroup>
14+
</Project>

StreamGuard.Console/app.manifest

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
3+
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
4+
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
5+
<security>
6+
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
7+
<!-- UAC Manifest Options
8+
If you want to change the Windows User Account Control level replace the
9+
requestedExecutionLevel node with one of the following.
10+
11+
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
12+
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
13+
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
14+
15+
Specifying requestedExecutionLevel element will disable file and registry virtualization.
16+
Remove this element if your application requires this virtualization for backwards
17+
compatibility.
18+
-->
19+
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
20+
</requestedPrivileges>
21+
</security>
22+
</trustInfo>
23+
24+
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
25+
<application>
26+
<!-- A list of the Windows versions that this application has been tested on
27+
and is designed to work with. Uncomment the appropriate elements
28+
and Windows will automatically select the most compatible environment. -->
29+
30+
<!-- Windows Vista -->
31+
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->
32+
33+
<!-- Windows 7 -->
34+
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->
35+
36+
<!-- Windows 8 -->
37+
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->
38+
39+
<!-- Windows 8.1 -->
40+
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->
41+
42+
<!-- Windows 10 -->
43+
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->
44+
45+
</application>
46+
</compatibility>
47+
48+
<!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
49+
DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need
50+
to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should
51+
also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config.
52+
53+
Makes the application long-path aware. See https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation -->
54+
<!--
55+
<application xmlns="urn:schemas-microsoft-com:asm.v3">
56+
<windowsSettings>
57+
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
58+
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
59+
</windowsSettings>
60+
</application>
61+
-->
62+
63+
<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
64+
<!--
65+
<dependency>
66+
<dependentAssembly>
67+
<assemblyIdentity
68+
type="win32"
69+
name="Microsoft.Windows.Common-Controls"
70+
version="6.0.0.0"
71+
processorArchitecture="*"
72+
publicKeyToken="6595b64144ccf1df"
73+
language="*"
74+
/>
75+
</dependentAssembly>
76+
</dependency>
77+
-->
78+
79+
</assembly>

0 commit comments

Comments
 (0)