浏览代码

优化解析程序,开启多任务

callm 5 年之前
父节点
当前提交
402e020920
共有 3 个文件被更改,包括 63 次插入40 次删除
  1. 13 4
      FileWatcher/Form1.Designer.cs
  2. 47 36
      FileWatcher/Form1.cs
  3. 3 0
      FileWatcher/Form1.resx

+ 13 - 4
FileWatcher/Form1.Designer.cs

@@ -28,8 +28,10 @@
         /// </summary>
         private void InitializeComponent()
         {
+            this.components = new System.ComponentModel.Container();
             this.FileWatcher = new System.IO.FileSystemWatcher();
             this.richTextBox1 = new System.Windows.Forms.RichTextBox();
+            this.timer1 = new System.Windows.Forms.Timer(this.components);
             ((System.ComponentModel.ISupportInitialize)(this.FileWatcher)).BeginInit();
             this.SuspendLayout();
             // 
@@ -44,18 +46,24 @@
             this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
             | System.Windows.Forms.AnchorStyles.Left) 
             | System.Windows.Forms.AnchorStyles.Right)));
-            this.richTextBox1.Location = new System.Drawing.Point(12, 12);
+            this.richTextBox1.Location = new System.Drawing.Point(24, 24);
+            this.richTextBox1.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6);
             this.richTextBox1.Name = "richTextBox1";
-            this.richTextBox1.Size = new System.Drawing.Size(260, 237);
+            this.richTextBox1.Size = new System.Drawing.Size(516, 470);
             this.richTextBox1.TabIndex = 0;
             this.richTextBox1.Text = "";
             // 
+            // timer1
+            // 
+            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
+            // 
             // Form1
             // 
-            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
+            this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 24F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
-            this.ClientSize = new System.Drawing.Size(284, 261);
+            this.ClientSize = new System.Drawing.Size(568, 522);
             this.Controls.Add(this.richTextBox1);
+            this.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6);
             this.Name = "Form1";
             this.Text = "Form1";
             this.Load += new System.EventHandler(this.Form1_Load);
@@ -68,6 +76,7 @@
 
         private System.IO.FileSystemWatcher FileWatcher;
         private System.Windows.Forms.RichTextBox richTextBox1;
+        private System.Windows.Forms.Timer timer1;
     }
 }
 

+ 47 - 36
FileWatcher/Form1.cs

@@ -16,7 +16,7 @@ namespace FileWatcher
 
         private void Form1_Load(object sender, EventArgs e)
         {
-            FileWatcher.Path = @"D:\";
+            FileWatcher.Path = @"C:\";
             FileWatcher.Filter = "*.jdf";
             FileWatcher.EnableRaisingEvents = true;
             FileWatcher.Created += new FileSystemEventHandler(Watcher_Created);
@@ -27,51 +27,53 @@ namespace FileWatcher
             rk2.SetValue("FileWatcher.exe", path);
             rk2.Close();
             rk.Close();
+            timer1.Interval = 1800000;
+            timer1.Start();
+
         }
 
         private void Watcher_Created(object sender, FileSystemEventArgs e)
         {
-            using (var process = new Process())
+            if (!e.FullPath.Contains("RECYCLE"))
             {
-                if (!e.FullPath.Contains("RECYCLE"))
+                string FullName = e.FullPath;
+                string Filename = FullName.Substring(FullName.LastIndexOf(@"\") + 1).Split('.')[0];
+                string StartPath = FullName.Substring(0, FullName.LastIndexOf(@"\") + 1);
+                if (File.Exists(FullName))
                 {
-                    string FullName = e.FullPath;
-                    process.StartInfo.Arguments = @"D:\FileWatcher\DTS-JDFData2Excel.exe " + FullName;
-                    string Filename = FullName.Substring(FullName.LastIndexOf(@"\") + 1).Split('.')[0];
-                    string StartPath = FullName.Substring(0, FullName.LastIndexOf(@"\") + 1);
-                    if (File.Exists(FullName))
-                    {
-                        richTextBox1.AppendText(DateTime.Now.ToString("yyyy/MM/dd h:mm:ss.fff") + e.FullPath + "\n");
-                        DoLog(FullName);
-                    }
-                    else
-                    {
-                        richTextBox1.AppendText("不存在文件" + FullName + "\n");
-                    }
-                    //不存在同名的文件则进行转换
-                    if (!File.Exists(StartPath + Filename + ".xls") && File.Exists(FullName))
-                    {
-                        process.StartInfo.FileName = @"D:\FileWatcher\DTS-JDFData2Excel.exe";
-                        process.StartInfo.UseShellExecute = false;
-                        process.StartInfo.RedirectStandardInput = true;
-                        process.StartInfo.RedirectStandardOutput = true;
-                        process.StartInfo.RedirectStandardError = true;
-                        process.StartInfo.CreateNoWindow = true;
-
-                        process.Start();
-                        process.StandardInput.AutoFlush = true;
-                        process.StandardInput.WriteLine("exit");
-                        //获取cmd窗口的输出信息  
-                        string output = process.StandardOutput.ReadToEnd();
-
-                        process.WaitForExit();
-                        process.Close();
-                    }
+                    richTextBox1.AppendText(DateTime.Now.ToString("yyyy/MM/dd h:mm:ss.fff") + e.FullPath + "\n");
+                    DoLog(FullName);
+                }
+                else
+                {
+                    richTextBox1.AppendText("不存在文件" + FullName + "\n");
+                }
+                //不存在同名的文件则进行转换
+                if (!File.Exists(StartPath + Filename + ".xls") && File.Exists(FullName))
+                {
+                    exec(@"C:\FileWatcher\DTS-JDFData2Excel.exe", @"C:\FileWatcher\DTS-JDFData2Excel.exe " + FullName);
                 }
             }
         }
 
-        public  void DoLog(string Message)
+        public void exec(string exePath, string parameters)
+        {
+            Process process = new System.Diagnostics.Process();
+            process.StartInfo.FileName = exePath;
+            process.StartInfo.Arguments = parameters;
+            process.StartInfo.UseShellExecute = false;
+            process.StartInfo.CreateNoWindow = true;
+            process.StartInfo.RedirectStandardOutput = true;
+            process.BeginOutputReadLine();
+            process.OutputDataReceived += new DataReceivedEventHandler(Process_OutputDataReceived);
+        }
+
+        private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
+        {
+
+        }
+
+        public void DoLog(string Message)
         {
             try
             {
@@ -138,5 +140,14 @@ namespace FileWatcher
                 throw ex;
             }
         }
+
+        private void timer1_Tick(object sender, EventArgs e)
+        {
+            Process[] processes1 = Process.GetProcessesByName("DTS-JDFData2Excel");
+            for (int i = 0; i < processes1.Length; i++)
+            {
+                processes1[i].Kill();
+            }
+        }
     }
 }

+ 3 - 0
FileWatcher/Form1.resx

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