lith 5 anos atrás
pai
commit
66ade2af67

+ 5 - 0
dotnet/Doc/UpgradeLog/Sers2.1.1.txt

@@ -105,3 +105,8 @@ ServiceCenter
 
 [tag]Sers2.1.1.329
 ------------------------------------------------------------------------------------------------------------------
+修复 [Sers.Hardware]OsShell.Shell 异常退出时不会关闭打开的进程的bug
+
+[tag]Sers2.1.1.346
+------------------------------------------------------------------------------------------------------------------
+

+ 115 - 13
dotnet/Sers/Sers.Hardware/Sers.Hardware/OsShell.cs

@@ -1,4 +1,5 @@
-using System.Diagnostics;
+using System;
+using System.Diagnostics;
 
 namespace Sers.Hardware
 {
@@ -10,23 +11,80 @@ namespace Sers.Hardware
         /// <param name="fileName"> 如 reboot </param>
         /// <param name="arguments"></param>
         /// <param name="output"></param>
+        /// <param name="millisecondsOfWait">等待时间,默认4000。若不指定则永久等待</param>
         /// <returns></returns>
-        public static void Shell(string fileName,string arguments,out string output)
+        public static void Shell(string fileName, string arguments, out string output, int? millisecondsOfWait = 4000)
         {
             output = null;
-            using (var process = new Process
+
+            //(x.1)创建Process
+            var process = new Process
             {
                 StartInfo = new ProcessStartInfo(fileName, arguments)
                 {
                     RedirectStandardOutput = true,
                     UseShellExecute = false
                 }
-            })
+            };
+
+
+            #region (x.2)ProcessExit,确保程序退出时会关闭process          
+            EventHandler stopProcess = null;
+            stopProcess = (s, e) =>
+            {
+                lock (stopProcess)
+                {
+                    AppDomain.CurrentDomain.ProcessExit -= stopProcess;
+                    try
+                    {
+                        if (process != null)
+                        {
+                            if (!process.HasExited)
+                            {
+                                process.Kill();
+                            }
+
+                            process.Dispose();
+                            process = null;
+                        }
+                    }
+                    catch (System.Exception ex)
+                    {
+                    }
+                }
+            };
+            AppDomain.CurrentDomain.ProcessExit += stopProcess;
+            #endregion
+
+
+            #region (x.3)启动Process           
+            try
             {
+
                 process.Start();
-                output = process.StandardOutput.ReadToEnd();
-                process.WaitForExit();
-            }  
+                //process.WaitForExit(millisecondsOfWait);
+                //process.WaitForExit();
+                //output = process.StandardOutput.ReadToEnd();
+                var task = process.StandardOutput.ReadToEndAsync();
+                if (millisecondsOfWait.HasValue)
+                {
+                    task.Wait(millisecondsOfWait.Value); 
+                }
+                else
+                {
+                    task.Wait();
+                }
+                if (task.IsCompleted)
+                {
+                    output = task.Result;
+                }
+            }
+            finally
+            {
+                stopProcess(null, null);
+            }
+            #endregion
+
 
         }
 
@@ -35,22 +93,66 @@ namespace Sers.Hardware
         /// </summary>
         /// <param name="fileName"> 如 reboot </param>
         /// <param name="arguments"></param>
+        /// <param name="millisecondsOfWait">等待时间,默认null。若不指定则永久等待</param>
         /// <returns></returns>
-        public static void Shell(string fileName, string arguments)
+        public static void Shell(string fileName, string arguments, int? millisecondsOfWait = null)
         {
-       
-            using (var process = new Process
+
+            //(x.1)创建Process
+            var process = new Process
             {
                 StartInfo = new ProcessStartInfo(fileName, arguments)
                 {
                     //RedirectStandardOutput = true,
                     UseShellExecute = false
                 }
-            })
+            };
+
+
+            #region (x.2)ProcessExit,确保程序退出时会关闭process          
+            EventHandler stopProcess = null;
+            stopProcess = (s, e) =>
+            {
+                lock (stopProcess)
+                {
+                    AppDomain.CurrentDomain.ProcessExit -= stopProcess;
+                    try
+                    {
+                        if (process != null)
+                        {
+                            if (!process.HasExited)
+                            {
+                                process.Kill();
+                            }
+
+                            process.Dispose();
+                            process = null;
+                        }
+                    }
+                    catch (System.Exception ex)
+                    {
+                    }
+                }
+            };
+            AppDomain.CurrentDomain.ProcessExit += stopProcess;
+            #endregion
+
+
+            #region (x.3)启动Process           
+            try
             {
-                process.Start();          
-                process.WaitForExit();
+                process.Start();
+                if (millisecondsOfWait.HasValue)
+                    process.WaitForExit(millisecondsOfWait.Value);
+                else
+                    process.WaitForExit();
+            }
+            finally
+            {
+                stopProcess(null, null);
             }
+            #endregion
+
 
         }
     }

+ 1 - 0
dotnet/Sers/Sers.Hardware/Sers.Hardware/Usage/WindowsUsageReader.cs

@@ -9,6 +9,7 @@ namespace Sers.Hardware.Usage
 
         public void Start()
         {
+            return;
             try
             {
                 Dispose();