From a6fbfa9cd71f2666a1f6927927ffe85d79118cd7 Mon Sep 17 00:00:00 2001 From: LarsBrubaker Date: Sun, 20 Dec 2020 16:33:58 -0800 Subject: [PATCH] make sure we don't sleep when printing --- .../WindowsPlatformsFeatures.cs | 10 +++---- .../ApplicationView/ApplicationController.cs | 9 ++++++ Program.cs | 29 +++++++++++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/MatterControl.Winforms/WindowsPlatformsFeatures.cs b/MatterControl.Winforms/WindowsPlatformsFeatures.cs index a495e1cdd..8bd82a084 100644 --- a/MatterControl.Winforms/WindowsPlatformsFeatures.cs +++ b/MatterControl.Winforms/WindowsPlatformsFeatures.cs @@ -47,7 +47,7 @@ namespace MatterHackers.MatterControl public void TakePhoto(string imageFileName) { - ImageBuffer noCameraImage = new ImageBuffer(640, 480); + var noCameraImage = new ImageBuffer(640, 480); Graphics2D graphics = noCameraImage.NewGraphics2D(); graphics.Clear(Color.White); graphics.DrawString("No Camera Detected", 320, 240, pointSize: 24, justification: Agg.Font.Justification.Center); @@ -59,14 +59,14 @@ namespace MatterHackers.MatterControl public void OpenCameraPreview() { - //Camera launcher placeholder (KP) + // Camera launcher placeholder (KP) if (ApplicationSettings.Instance.get(ApplicationSettingsKey.HardwareHasCamera) == "true") { - //Do something + // Do something } else { - //Do something else (like show warning message) + // Do something else (like show warning message) } } @@ -76,7 +76,7 @@ namespace MatterHackers.MatterControl { using (var mediaStream = StaticData.Instance.OpenStream(Path.Combine("Sounds", fileName))) { - (new System.Media.SoundPlayer(mediaStream)).Play(); + new System.Media.SoundPlayer(mediaStream).Play(); } } } diff --git a/MatterControlLib/ApplicationView/ApplicationController.cs b/MatterControlLib/ApplicationView/ApplicationController.cs index 34aa1840d..8e48d3282 100644 --- a/MatterControlLib/ApplicationView/ApplicationController.cs +++ b/MatterControlLib/ApplicationView/ApplicationController.cs @@ -1978,6 +1978,7 @@ namespace MatterHackers.MatterControl } public bool Allow32BitReSlice { get; set; } + public Action KeepAwake { get; set; } /// /// Archives MCX and validates GCode results before starting a print operation @@ -2233,8 +2234,14 @@ namespace MatterHackers.MatterControl UiThread.RunOnIdle(() => this.ShellFileOpened?.Invoke(this, file)); } + private void KeepAwakeIfNeeded() + { + KeepAwake?.Invoke(AnyPrintTaskRunning); + } + public void Connection_PrintStarted(object sender, EventArgs e) { + KeepAwakeIfNeeded(); AnyPrintStarted?.Invoke(sender, e); } @@ -2248,6 +2255,7 @@ namespace MatterHackers.MatterControl printHistoryEditor.CollectInfoPrintFinished(); } + KeepAwakeIfNeeded(); AnyPrintComplete?.Invoke(sender, null); } @@ -2261,6 +2269,7 @@ namespace MatterHackers.MatterControl printHistoryEditor.CollectInfoPrintCanceled(); } + KeepAwakeIfNeeded(); AnyPrintCanceled?.Invoke(sender, e); } diff --git a/Program.cs b/Program.cs index a41839ddb..133c89e64 100644 --- a/Program.cs +++ b/Program.cs @@ -50,6 +50,20 @@ namespace MatterHackers.MatterControl { public class Program { + [FlagsAttribute] + public enum EXECUTION_STATE : uint + { + ES_AWAYMODE_REQUIRED = 0x00000040, + ES_CONTINUOUS = 0x80000000, + ES_DISPLAY_REQUIRED = 0x00000002, + ES_SYSTEM_REQUIRED = 0x00000001 + // Legacy flag, should not be used. + // ES_USER_PRESENT = 0x00000004 + } + + [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] + static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags); + private static EventWaitHandle waitHandle; private const int RaygunMaxNotifications = 15; @@ -242,6 +256,8 @@ namespace MatterHackers.MatterControl theme.TabBarBackground, new Color(theme.PrimaryAccentColor, 175)); + ApplicationController.Instance.KeepAwake = KeepAwake; + systemWindow.ShowAsSystemWindow(); } @@ -249,6 +265,19 @@ namespace MatterHackers.MatterControl private static readonly object locker = new object(); + static void KeepAwake(bool keepAwake) + { + if (keepAwake) + { + // Prevent Idle-to-Sleep + SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_DISPLAY_REQUIRED); + } + else + { + SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS); + } + } + public class LocalService : IMainService { public void ShellOpenFile(string[] files)