Browse Source

定时任务转换Excel,杀掉执行时间过长的转换程序

callm 5 years ago
parent
commit
c8207d6505
3 changed files with 120 additions and 7 deletions
  1. 9 3
      FileWatcher/Form1.Designer.cs
  2. 108 4
      FileWatcher/Form1.cs
  3. 3 0
      FileWatcher/Form1.resx

+ 9 - 3
FileWatcher/Form1.Designer.cs

@@ -32,6 +32,7 @@
             this.FileWatcher = new System.IO.FileSystemWatcher();
             this.FileWatcher = new System.IO.FileSystemWatcher();
             this.richTextBox1 = new System.Windows.Forms.RichTextBox();
             this.richTextBox1 = new System.Windows.Forms.RichTextBox();
             this.timer1 = new System.Windows.Forms.Timer(this.components);
             this.timer1 = new System.Windows.Forms.Timer(this.components);
+            this.timer2 = new System.Windows.Forms.Timer(this.components);
             ((System.ComponentModel.ISupportInitialize)(this.FileWatcher)).BeginInit();
             ((System.ComponentModel.ISupportInitialize)(this.FileWatcher)).BeginInit();
             this.SuspendLayout();
             this.SuspendLayout();
             // 
             // 
@@ -47,9 +48,9 @@
             | System.Windows.Forms.AnchorStyles.Left) 
             | System.Windows.Forms.AnchorStyles.Left) 
             | System.Windows.Forms.AnchorStyles.Right)));
             | System.Windows.Forms.AnchorStyles.Right)));
             this.richTextBox1.Location = new System.Drawing.Point(24, 24);
             this.richTextBox1.Location = new System.Drawing.Point(24, 24);
-            this.richTextBox1.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6);
+            this.richTextBox1.Margin = new System.Windows.Forms.Padding(6);
             this.richTextBox1.Name = "richTextBox1";
             this.richTextBox1.Name = "richTextBox1";
-            this.richTextBox1.Size = new System.Drawing.Size(516, 470);
+            this.richTextBox1.Size = new System.Drawing.Size(516, 444);
             this.richTextBox1.TabIndex = 0;
             this.richTextBox1.TabIndex = 0;
             this.richTextBox1.Text = "";
             this.richTextBox1.Text = "";
             // 
             // 
@@ -57,13 +58,17 @@
             // 
             // 
             this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
             this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
             // 
             // 
+            // timer2
+            // 
+            this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
+            // 
             // Form1
             // Form1
             // 
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 24F);
             this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 24F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(568, 522);
             this.ClientSize = new System.Drawing.Size(568, 522);
             this.Controls.Add(this.richTextBox1);
             this.Controls.Add(this.richTextBox1);
-            this.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6);
+            this.Margin = new System.Windows.Forms.Padding(6);
             this.Name = "Form1";
             this.Name = "Form1";
             this.Text = "Form1";
             this.Text = "Form1";
             this.Load += new System.EventHandler(this.Form1_Load);
             this.Load += new System.EventHandler(this.Form1_Load);
@@ -77,6 +82,7 @@
         private System.IO.FileSystemWatcher FileWatcher;
         private System.IO.FileSystemWatcher FileWatcher;
         private System.Windows.Forms.RichTextBox richTextBox1;
         private System.Windows.Forms.RichTextBox richTextBox1;
         private System.Windows.Forms.Timer timer1;
         private System.Windows.Forms.Timer timer1;
+        private System.Windows.Forms.Timer timer2;
     }
     }
 }
 }
 
 

+ 108 - 4
FileWatcher/Form1.cs

@@ -3,6 +3,7 @@ using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Diagnostics;
 using System.IO;
 using System.IO;
+using System.Threading;
 using System.Windows.Forms;
 using System.Windows.Forms;
 
 
 namespace FileWatcher
 namespace FileWatcher
@@ -27,8 +28,12 @@ namespace FileWatcher
             rk2.SetValue("FileWatcher.exe", path);
             rk2.SetValue("FileWatcher.exe", path);
             rk2.Close();
             rk2.Close();
             rk.Close();
             rk.Close();
-            timer1.Interval = 600000;
+            timer1.Interval = 1000 * 10;
             timer1.Start();
             timer1.Start();
+
+            timer2.Interval = 1000 * 60 * 60;
+            timer2.Start();
+            RunTran();
         }
         }
 
 
         private void Watcher_Created(object sender, FileSystemEventArgs e)
         private void Watcher_Created(object sender, FileSystemEventArgs e)
@@ -42,7 +47,7 @@ namespace FileWatcher
                     string StartPath = FullName.Substring(0, FullName.LastIndexOf(@"\") + 1);
                     string StartPath = FullName.Substring(0, FullName.LastIndexOf(@"\") + 1);
                     if (File.Exists(FullName))
                     if (File.Exists(FullName))
                     {
                     {
-                        richTextBox1.AppendText(DateTime.Now.ToString("yyyy/MM/dd h:mm:ss.fff") + e.FullPath + "\n");
+                        richTextBox1.AppendText(DateTime.Now.ToString("yyyy/MM/dd h:mm:ss.fff") + " " + e.FullPath + "\n");
                         DoLog(FullName);
                         DoLog(FullName);
                     }
                     }
                     else
                     else
@@ -61,6 +66,10 @@ namespace FileWatcher
                             exec(@"D:\FileWatcher\NJDF\DTS-Data2Excel.exe", @"D:\FileWatcher\NJDF\DTS-Data2Excel.exe " + FullName);
                             exec(@"D:\FileWatcher\NJDF\DTS-Data2Excel.exe", @"D:\FileWatcher\NJDF\DTS-Data2Excel.exe " + FullName);
                         }
                         }
                     }
                     }
+                    else
+                    {
+                        richTextBox1.AppendText(DateTime.Now.ToString("yyyy/MM/dd h:mm:ss.fff") + " 已存在Excel文件 " + e.FullPath + "\n");
+                    }
                 }
                 }
             }
             }
         }
         }
@@ -154,14 +163,109 @@ namespace FileWatcher
             Process[] processes1 = Process.GetProcessesByName("DTS-JDFData2Excel");
             Process[] processes1 = Process.GetProcessesByName("DTS-JDFData2Excel");
             for (int i = 0; i < processes1.Length; i++)
             for (int i = 0; i < processes1.Length; i++)
             {
             {
-                processes1[i].Kill();
+                //杀掉超过20秒没关闭的进程
+                if (DateTime.Now > processes1[1].StartTime.AddSeconds(20))
+                {
+                    processes1[i].Kill();
+                }
             }
             }
 
 
             Process[] processes2 = Process.GetProcessesByName("DTS-Data2Excel");
             Process[] processes2 = Process.GetProcessesByName("DTS-Data2Excel");
             for (int i = 0; i < processes2.Length; i++)
             for (int i = 0; i < processes2.Length; i++)
             {
             {
-                processes2[i].Kill();
+                if (DateTime.Now > processes2[1].StartTime.AddSeconds(20))
+                {
+                    processes2[i].Kill();
+                }
+            }
+        }
+
+        private void RunTran()
+        {
+            try
+            {
+                List<FileInfo> lst = new List<FileInfo>();
+                string[] path = new string[] { @"D:\PROB-001", @"D:\PROB-002", @"D:\PROB-003", @"D:\PROB-004",
+                @"D:\PROB-005", @"D:\PROB-006", @"D:\PROB-007", @"D:\PROB-008", @"D:\PROB-009",
+                @"D:\PROB-010", @"D:\PROB-011", @"D:\PROB-012", @"D:\PROB-013", @"D:\PROB-014",
+                @"D:\PROB-015", @"D:\PROB-004", @"D:\PROB-016", @"D:\PROB-017", @"D:\PROB-018",
+                @"D:\PROB-019", @"D:\PROB-020", @"D:\PROB-021", @"D:\PROB-022", @"D:\PROB-023",
+                @"D:\PROB-024", @"D:\PROB-026",@"D:\PROB-027",@"D:\PROB-028",@"D:\PROB-029",
+                @"D:\PROB-030",@"D:\PROB-031",@"D:\PROB-032",@"D:\PROB-033",@"D:\PROB-034",
+                @"D:\PROB-035",@"D:\PROB-036",@"D:\PROB-037",@"D:\PROB-038",@"D:\PROB-039",
+            @"D:\PROB-040",@"D:\PROB-041",@"D:\PROB-042",@"D:\PROB-043",@"D:\PROB-045",@"D:\PROB-046",
+            @"D:\PROB-047",@"D:\PROB-048",@"D:\PROB-049",@"D:\PROB-050",@"D:\PROB-051",@"D:\PROB-052",@"D:\PROB-053",
+            @"D:\PROB-054",@"D:\PROB-055",@"D:\PROB-056",@"D:\PROB-057",@"D:\PROB-058",@"D:\PROB-059",@"D:\PROB-060",
+            @"D:\PROB-061",@"D:\PROB-062",@"D:\PROB-063",@"D:\PROB-064",@"D:\PROB-65",@"D:\PROB-66",@"D:\PROB-067",
+            @"D:\PROB-068",@"D:\PROB-069",@"D:\PROB-070",@"D:\PROB-071",@"D:\PROB-072",@"D:\PROB-073",@"D:\PROB-074",
+            @"D:\PROB-075",@"D:\PROB-76",@"D:\PROB-077",@"D:\PROB-078",@"D:\PROB-079",@"D:\PROB-080",@"D:\PROB-081",@"D:\PROB-082"};
+
+                //string[] path = textBox1.Text.Split(',');
+                for (int i = 0; i < path.Length; i++)
+                {
+                    richTextBox1.AppendText(path[i] + "\n");
+                    List<FileInfo> lstFiles = getFile(path[i], ".jdf", lst);
+                    foreach (FileInfo shpFile in lstFiles)
+                    {
+                        string Filename = shpFile.FullName.Substring(shpFile.FullName.LastIndexOf(@"\") + 1).Split('.')[0];
+                        string StartPath = shpFile.FullName.Substring(0, shpFile.FullName.LastIndexOf(@"\") + 1);
+                        //不存在同名的文件则进行转换
+                        if (!File.Exists(StartPath + Filename + ".xls"))
+                        {
+                            Thread.Sleep(1000);
+                            exec(@"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe", @"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe " + shpFile.FullName);
+                            richTextBox1.AppendText(shpFile.FullName + "\n");
+                        }
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                richTextBox1.AppendText(ex.Message + ex.StackTrace + "\n");
+            }
+        }
+
+
+        /// <summary>
+        /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
+        /// </summary>
+        /// <param name="path">文件夹路径</param>
+        /// <param name="extName">扩展名可以多个 例如 .mp3.wma.rm</param>
+        /// <returns>List<FileInfo></returns>
+        public static List<FileInfo> getFile(string path, string extName, List<FileInfo> lst)
+        {
+            try
+            {
+
+                string[] dir = Directory.GetDirectories(path); //文件夹列表  
+                DirectoryInfo fdir = new DirectoryInfo(path);
+                FileInfo[] file = fdir.GetFiles();
+                //FileInfo[] file = Directory.GetFiles(path); //文件列表  
+                if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空          
+                {
+                    foreach (FileInfo f in file) //显示当前目录所有文件  
+                    {
+                        if (extName.ToLower().IndexOf(f.Extension.ToLower()) >= 0)
+                        {
+                            lst.Add(f);
+                        }
+                    }
+                    foreach (string d in dir)
+                    {
+                        getFile(d, extName, lst);//递归  
+                    }
+                }
+                return lst;
+            }
+            catch (Exception ex)
+            {
+                throw ex;
             }
             }
         }
         }
+
+        private void timer2_Tick(object sender, EventArgs e)
+        {
+            RunTran();
+        }
     }
     }
 }
 }

+ 3 - 0
FileWatcher/Form1.resx

@@ -123,4 +123,7 @@
   <metadata name="timer1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
   <metadata name="timer1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
     <value>228, 17</value>
     <value>228, 17</value>
   </metadata>
   </metadata>
+  <metadata name="timer2.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+    <value>376, 17</value>
+  </metadata>
 </root>
 </root>