Skip to content

Commit c07ed20

Browse files
committed
Prevent system sleep during broadcast module operation
Added SetSystemSleepState method using SetThreadExecutionState to control system sleep behavior when starting broadcast modules. This ensures the system does not enter sleep mode while broadcasting is active, improving reliability.
1 parent a84d7cb commit c07ed20

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

RapidMessageCast/RapidMessageCast GUI/Internal RMC Components/BroadcastController.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using RapidMessageCast_Manager.BroadcastModules;
22
using System.Diagnostics;
3-
3+
using System.Runtime.InteropServices;
44
//--RapidMessageCast Software--
55
//BroadcastController.cs - RapidMessageCast Manager
66
//Used to control and watch over the broadcast modules. This class is used to start and stop the modules, as well as check if any are running.
@@ -30,6 +30,14 @@ namespace RapidMessageCast_Manager.Internal_RMC_Components
3030
{
3131
internal class BroadcastController
3232
{
33+
// Import the SetThreadExecutionState function from kernel32.dll
34+
[DllImport("kernel32.dll")]
35+
private static extern uint SetThreadExecutionState(uint esFlags);
36+
37+
// Flags to prevent the system from sleeping
38+
private const uint ES_CONTINUOUS = 0x80000000;
39+
private const uint ES_SYSTEM_REQUIRED = 0x00000001;
40+
3341
private static readonly Dictionary<RMCEnums, bool> moduleRunning = new()
3442
{
3543
{ RMCEnums.PC, false },
@@ -47,6 +55,7 @@ public async Task StartPCBroadcastModule(string message = "", string computerLis
4755
MessageBox.Show("Fatal Error - StartBroadcastModule has reported a critical error, it is recommended that you restart RapidMessageCast. BroadcastController reported as null.");
4856
return;
4957
}
58+
SetSystemSleepState(RMCManagerForm, false);
5059
PCBroadcastModule pcBroadcastModule = new();
5160
RMCManagerForm.TraceLog($"Info - [BroadcastController]: Starting StartPCBroadcastModule...");
5261
moduleRunning[RMCEnums.PC] = true;
@@ -192,5 +201,27 @@ private static void RMCDebugCall(RMCManager RMCManagerForm, string classCall)
192201
RMCManagerForm.TraceLog("Critical - [RMCDebugCall] - Detailed log: RMC process virtual memory: " + Process.GetCurrentProcess().VirtualMemorySize64);
193202
RMCManagerForm.TraceLog("Critical - [RMCDebugCall] - Debug call completed.");
194203
}
204+
205+
private static void SetSystemSleepState(RMCManager RMCManagerForm, bool AllowSleep)
206+
{
207+
RMCManagerForm.TraceLog($"Info - [SetSystemSleepState] - Attempting to set sleep state to: {AllowSleep} ...");
208+
try
209+
{
210+
if (AllowSleep) // System can sleep
211+
{
212+
SetThreadExecutionState(ES_CONTINUOUS);
213+
RMCManagerForm.TraceLog("Info - [SetSystemSleepState] - System sleep is now allowed.");
214+
}
215+
else //No, prevent sleep
216+
{
217+
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
218+
RMCManagerForm.TraceLog("Info - [SetSystemSleepState] - System sleep is disabled.");
219+
}
220+
}
221+
catch (Exception ex)
222+
{
223+
RMCManagerForm.TraceLog("Error - [SetSystemSleepState] - An error occurred while trying to set the system sleep state. " + ex.Message);
224+
}
225+
}
195226
}
196227
}

0 commit comments

Comments
 (0)