Browse Source

Inhibit computer power management and block the screen saver.

Gregory J Huxley 1 month ago
parent
commit
fb0f87a885

+ 1 - 0
crusherScanner/Form1.Designer.cs

@@ -18,6 +18,7 @@
         {
             if (disposing && (components != null))
             {
+                SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
                 components.Dispose();
             }
             base.Dispose(disposing);

+ 62 - 1
crusherScanner/Form1.cs

@@ -1,7 +1,63 @@
+using System.Runtime.InteropServices;
+
 namespace crusherScanner
 {
     public partial class Form1 : Form
     {
+        /// <summary>
+        /// Set the execution state requried for the application.
+        /// </summary>
+        [FlagsAttribute]
+        public enum EXECUTION_STATE : uint
+        {
+            /// <summary>
+            /// Enum value for setting the execution state to active without requiring the screen to be on.
+            /// </summary>
+            ES_AWAYMODE_REQUIRED = 0x00000040,
+            /// <summary>
+            /// Enum value to latch the execution state to the active flags or clear them with no flags.
+            /// </summary>
+            ES_CONTINUOUS = 0x80000000,
+            /// <summary>
+            /// Enum value for setting the display state to active.
+            /// </summary>
+            ES_DISPLAY_REQUIRED = 0x00000002,
+            /// <summary>
+            /// Enum value for prevening the system going to sleep.
+            /// </summary>
+            ES_SYSTEM_REQUIRED = 0x00000001,
+            /// <summary>
+            /// Legacy flag, should not be used.
+            /// </summary>
+            ES_USER_PRESENT = 0x00000004
+        }
+
+        /// <summary>
+        /// Access the system parameter info handler.
+        /// </summary>
+        [FlagsAttribute]
+        public enum ParametersInfo : uint
+        {
+            /// <summary>
+            /// Get the current screen savers timeout counter.
+            /// </summary>
+            SPI_GETSCREENSAVETIMEOUT = 0x000E,
+            /// <summary>
+            /// Set the current screen savers timeout counter.
+            /// </summary>
+            SPI_SETSCREENSAVETIMEOUT = 0x000F,
+            /// <summary>
+            /// Call update of ParameterInfo.
+            /// </summary>
+            SPIF_SENDCHANGE = 0x02
+        }
+
+        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
+        static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
+        [DllImport("user32.dll", SetLastError = true)]
+        static extern bool SystemParametersInfo(ParametersInfo uiAction, int uiParam, IntPtr pvParam, ParametersInfo fWinIni);
+
+
         private char PathSeparator = '\\';
         private static ProgramFunctions? dataCore;
         private static int counter = 0;
@@ -216,7 +272,12 @@ namespace crusherScanner
             {
                 counter--;
             }
-             
+
+            // Set Therad state in windows so the computer doesn't go to sleep or turn the scrren off.
+            SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_SYSTEM_REQUIRED);
+            // Reset the screen saver timer to prevent the screen saver from starting.
+            nint timerValue = 0;
+            SystemParametersInfo(ParametersInfo.SPI_GETSCREENSAVETIMEOUT, 0, timerValue, ParametersInfo.SPIF_SENDCHANGE);
         }
 
         private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)

+ 13 - 14
crusherScanner/WorkingDirControl.cs

@@ -19,20 +19,20 @@ namespace crusherScanner
                 return;
             }
             Scan scanData = (Scan)e.Argument;
-            string[] DataDirs = new string[2] { Properties.Settings.Default.OreDefWorkFile, Properties.Settings.Default.OreDefInFile };
+            string[] DataDirs = [Properties.Settings.Default.OreDefWorkFile, Properties.Settings.Default.OreDefInFile];
             string? JobFile = GetJobFilePath(scanData, DataDirs);
             if (JobFile != null)
             {
                 if (JobFile.Contains(DataDirs[0]))
                 {
-                    scanData = updateSampleInJobFile(scanData, JobFile);
+                    scanData = UpdateSampleInJobFile(scanData, JobFile);
                 }
                 else
                 {
                     try
                     {
                         File.Move(JobFile, $"{DataDirs[0]}\\{scanData.sampleData.Job}.csv");
-                        scanData = updateSampleInJobFile(scanData, $"{DataDirs[0]}\\{scanData.sampleData.Job}.csv");
+                        scanData = UpdateSampleInJobFile(scanData, $"{DataDirs[0]}\\{scanData.sampleData.Job}.csv");
                     }
                     catch (Exception)
                     {
@@ -56,18 +56,17 @@ namespace crusherScanner
         {
             try
             {
-                foreach (var DataDir in DataDirs)
+                foreach (string DataDir in DataDirs)
                 {
                     if (Directory.Exists(DataDir))
                     {
                         // Pattern match is for 'ML003092.csv' or 'ML003092_20220822.csv'
                         var jobFiles = Directory.EnumerateFiles(DataDir, "ML??????*.csv", SearchOption.TopDirectoryOnly);
-                        foreach (var jobFile in jobFiles)
+                        foreach (string? jobFile in from jobFile in jobFiles
+                                                where jobFile.Contains(scanData.sampleData.Job)
+                                                select jobFile)
                         {
-                            if (jobFile.Contains(scanData.sampleData.Job))
-                            {
-                                return jobFile;
-                            }
+                            return jobFile;
                         }
                     }
                 }
@@ -79,20 +78,20 @@ namespace crusherScanner
             return null;
         }
 
-        private static Scan updateSampleInJobFile(Scan scanData, string DataFile)
+        private static Scan UpdateSampleInJobFile(Scan scanData, string DataFile)
         {
-            List<string> fileLines = new();
+            List<string> fileLines = [];
             using FileStream OreDefWorkingFile = new(DataFile, FileMode.Open, System.IO.FileAccess.ReadWrite, FileShare.None);
             {
                 using StreamReader Reader = new(OreDefWorkingFile);
                 {
-                    var line = Reader.ReadLine();
+                    string? line = Reader.ReadLine();
                     while (line != null)
                     {                       
                         if (line != null && line.Contains('\u002C'))
                         {
                             scanData.JobTotal++;
-                            string[] CSV = line.Split('\u002C');
+                            string[]? CSV = line.Split('\u002C');
                             if(CSV[0] == scanData.sampleData.Sid)
                             {
                                 if (scanData.Ins)
@@ -124,7 +123,7 @@ namespace crusherScanner
                 OreDefWorkingFile.Position = 0;
                 using StreamWriter writer = new(OreDefWorkingFile);
                 {
-                    foreach (var line in fileLines)
+                    foreach (string line in fileLines)
                     {
                         writer.WriteLine(line);
                     }

+ 5 - 5
crusherScanner/crusherScanner.csproj

@@ -2,11 +2,11 @@
 
   <PropertyGroup>
     <OutputType>WinExe</OutputType>
-    <TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
+    <TargetFramework>net9.0-windows10.0.17763.0</TargetFramework>
     <Nullable>enable</Nullable>
     <UseWindowsForms>true</UseWindowsForms>
     <ImplicitUsings>enable</ImplicitUsings>
-    <SignAssembly>True</SignAssembly>
+    <SignAssembly>False</SignAssembly>
     <ApplicationIcon>0d28e7dc-5024-45b7-81f8-bb1c48fe484d.ico</ApplicationIcon>
     <AssemblyOriginatorKeyFile>C:\Users\gregory\Documents\Visual Studio 2022\crusherScannerKeyPair.snk</AssemblyOriginatorKeyFile>
     <GenerateDocumentationFile>True</GenerateDocumentationFile>
@@ -57,9 +57,9 @@
   </ItemGroup>
 
   <ItemGroup>
-    <PackageReference Include="CsvHelper" Version="28.0.1" />
-    <PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
-    <PackageReference Include="System.IO.Ports" Version="6.0.0" />
+    <PackageReference Include="CsvHelper" Version="33.1.0" />
+    <PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
+    <PackageReference Include="System.IO.Ports" Version="9.0.9" />
   </ItemGroup>
 
   <ItemGroup>