Parcourir la source

打印机进程问题处理

Hcsy il y a 8 ans
Parent
commit
e33cde31a5

+ 4 - 0
UAS-MES/Entity/SystemInf.cs

@@ -48,5 +48,9 @@ namespace UAS_MES.Entity
         /// HeadBar的高度
         /// </summary>
         public static int HeadBarHeight = 0;
+        /// <summary>
+        /// 当前程序进程ID
+        /// </summary>
+        public static int ProcessesID;
     }
 }

+ 7 - 0
UAS-MES/FunctionCode/Make/Make_PalletWeigh.cs

@@ -69,6 +69,13 @@ namespace UAS_MES.Make
             try
             {
                 lbl = new ApplicationClass();
+                String str = SystemInf.ProcessesID + "|" + lbl.PID;
+                string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
+                FileStream fs = new FileStream(sysdisc + @":\" + "lblprocess" + ".txt", FileMode.Append, FileAccess.Write);
+                StreamWriter sw = new StreamWriter(fs);
+                sw.WriteLine(str, Encoding.UTF8);
+                sw.Close();
+                fs.Close();
             }
             catch (Exception ex)
             {

+ 1 - 0
UAS-MES/Login.cs

@@ -43,6 +43,7 @@ namespace UAS_MES
         {
             //设置默认数据库
             SystemInf.DefaultDB = Properties.Settings.Default.Properties["MES"].DefaultValue.ToString();
+            SystemInf.ProcessesID = System.Diagnostics.Process.GetCurrentProcess().Id;
             CheckForIllegalCrossThreadCalls = false;
             LoadMasterInf = new Thread(LoadMaster);
             LoadMasterInf.Start();

+ 43 - 3
UAS-MES/Main.cs

@@ -80,10 +80,50 @@ namespace UAS_MES
         private void Main_Load(object sender, EventArgs e)
         {
             //杀死全部未关闭的打印进程
-            Process[] processes = System.Diagnostics.Process.GetProcessesByName("lppa");
-            for (int i = 0; i < processes.Length; i++)
+            //Process[] processes = System.Diagnostics.Process.GetProcessesByName("lppa");
+            //for (int i = 0; i < processes.Length; i++)
+            //{
+            //    processes[i].Kill();
+            //}
+            string[] lines = System.IO.File.ReadAllLines(sysdisc + @":\" + "lblprocess" + ".txt");
+            foreach (string line in lines)
             {
-                processes[i].Kill();
+                if (line != "")
+                {
+                    string processid = line.Split('|')[0];
+                    string lblid = line.Split('|')[1];
+                    try
+                    {
+                        if (System.Diagnostics.Process.GetProcessById(int.Parse(processid)).ProcessName == System.Diagnostics.Process.GetCurrentProcess().ProcessName)
+                        {
+                        }
+                        else
+                        {
+                            System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
+                        }
+                        
+                    }
+                    catch (Exception)
+                    {
+                        try
+                        {
+                            System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
+                        }
+                        catch (Exception)
+                        {
+                        }
+                        Console.WriteLine("删除");
+                        FileStream fas = new FileStream(sysdisc + @":\" + "lblprocess" + ".txt", FileMode.Create, FileAccess.ReadWrite);
+                        StreamWriter sw = new StreamWriter(fas);
+                        StreamReader sr = new StreamReader(fas);
+                        String s = sr.ReadToEnd();
+                        sw.Write(s.Replace(line+"\r\n",""));
+                        sw.Flush();
+                        sw.Close();
+                        sr.Close();
+                        fas.Close();
+                    }              
+                }
             }
             thread = new Thread(DeleteLog);
             thread.Start();

+ 2 - 0
UAS-MES/Program.cs

@@ -41,6 +41,8 @@ namespace UAS_MES
                     Directory.CreateDirectory(sysdisc + @":\Log");
                 FileStream fs = new FileStream(sysdisc + @":\Log\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                 fs.Close();
+                FileStream fas = new FileStream(sysdisc + @":\" + "lblprocess" + ".txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
+                fas.Close();
                 if (principal.IsInRole(WindowsBuiltInRole.Administrator))
                     Application.Run(new Login());
                 else