Skip to content

Commit b0b7258

Browse files
committed
Prevent PC from sleeping while actively compressing. Closes #110
1 parent e126771 commit b0b7258

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

CompactGUI.Core/SharedMethods.vb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ Public Module SharedMethods
145145
Return oneDrivePaths.Any(Function(odPath) normalizedFolderPath.StartsWith(Path.GetFullPath(odPath).TrimEnd(Path.DirectorySeparatorChar).ToLowerInvariant()))
146146
End Function
147147

148+
Public Sub PreventSleep()
149+
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS Or EXECUTION_STATE.ES_SYSTEM_REQUIRED Or EXECUTION_STATE.ES_DISPLAY_REQUIRED)
150+
End Sub
151+
Public Sub RestoreSleep()
152+
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS)
153+
End Sub
154+
155+
148156
#Region "DLL Imports"
149157

150158
<DllImport("kernel32.dll", SetLastError:=True)>
@@ -173,6 +181,18 @@ Public Module SharedMethods
173181
End Function
174182

175183

184+
<DllImport("kernel32.dll", SetLastError:=True)>
185+
Private Function SetThreadExecutionState(esFlags As EXECUTION_STATE) As EXECUTION_STATE
186+
End Function
187+
188+
<Flags()>
189+
Private Enum EXECUTION_STATE As UInteger
190+
ES_AWAYMODE_REQUIRED = &H40
191+
ES_CONTINUOUS = &H80000000UI
192+
ES_DISPLAY_REQUIRED = &H2
193+
ES_SYSTEM_REQUIRED = &H1
194+
End Enum
195+
176196
#End Region
177197

178198

CompactGUI/ViewModels/HomeViewModel.vb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ Partial Public Class HomeViewModel
178178
Await Application.GetService(Of Watcher.Watcher).DisableBackgrounding()
179179

180180
Compressing = True
181+
Core.PreventSleep()
181182
Dim tasks As New List(Of Task)()
182183
Dim foldersToCompress = Folders.Where(Function(f) f.FolderActionState = ActionState.Idle).ToList
183184
For Each folder In foldersToCompress
@@ -209,6 +210,7 @@ Partial Public Class HomeViewModel
209210
AddOrUpdateFolderWatcher(folder)
210211
Next
211212

213+
Core.RestoreSleep()
212214

213215
Await Application.GetService(Of Watcher.Watcher).EnableBackgrounding()
214216
End Function

0 commit comments

Comments
 (0)