Procházet zdrojové kódy

修改不能正常关闭的bug

mengxiangge před 4 roky
rodič
revize
885ec0b7c3

+ 1 - 1
Dispatcher/Client/IRevitCommandExcutor.cs

@@ -15,6 +15,6 @@ namespace Client
         /// <param name="param">执行命令的参数</param>
         /// <param name="filePathList">用到的文件的路径</param>
         /// <returns>任务执行结果返回json格式字符串</returns>
-        string ExecuteCmd(string revitCmd, string param, HashSet<string> filePathList);
+        string ExecuteCmd(string revitCmd, string param, HashSet<string> filePathList, string taskId);
     }
 }

+ 31 - 8
Dispatcher/Client/Start/RevitCmdExecutor.cs

@@ -11,6 +11,7 @@ using System.IO;
 using System.Linq;
 using System.Windows.Forms;
 using Newtonsoft.Json.Linq;
+using System.Threading;
 
 namespace Client.Start
 {
@@ -19,7 +20,7 @@ namespace Client.Start
     /// </summary>
     public class RevitCmdExecutor : IRevitCommandExcutor
     {
-        public string ExecuteCmd(string revitCmd, string param, HashSet<string> filePathList)
+        public string ExecuteCmd(string revitCmd, string param, HashSet<string> filePathList, string taskId)
         {
             string msg = null;
             try
@@ -36,7 +37,7 @@ namespace Client.Start
                 process.StartInfo.RedirectStandardInput = true;//打开流输入
                 process.StartInfo.RedirectStandardOutput = true;//打开流输出
                 process.StartInfo.RedirectStandardError = true;//打开错误流
-                string resultFilePath = GetReusltFileName();
+                string resultFilePath = GetReusltFileName(taskId, revitCmd);
                 string newParam = AddResultFileNameToParam(param, resultFilePath);
                 Console.WriteLine(filePathList.First());
                 //输入参数,多个参数使用空间分割,如果一个参数包含空格,使用""包括此参数
@@ -45,11 +46,32 @@ namespace Client.Start
                                               "\"" + newParam + "\"" + " " +
                                               "\"" + filePathList.First() + "\"";
                 process.Start();//执行
+                process.WaitForExit();
                 //string msg = process.StandardOutput.ReadToEnd();//读取输出
-                process.WaitForExit(60*60*1000);//等待执行完成
-                //process.Close();//结束
-                if(!process.HasExited)
-                    process.Kill();
+                //try
+                //{
+                //    while (process != null && !process.HasExited)
+                //    {
+                //        process.WaitForExit(60 * 1000); //等待执行完成
+                //        if (!process.HasExited && File.Exists(resultFilePath))
+                //        {
+                //            Thread.Sleep(5000);
+                //            if (process != null && !process.HasExited)
+                //            {
+                //                //process.Close();
+                //                //process = null;
+                //                //process.Kill();
+                //                //process.Dispose();
+
+                //            }
+                //        }
+                //    }
+                //}
+                //catch(Exception ex)
+                //{
+                //    Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
+                //}
+                Thread.Sleep(15000);
                 msg = ReadResultString(resultFilePath);
                 
                 Console.WriteLine(DateTime.Now + " 命令执行完成:" + revitCmd);
@@ -94,10 +116,11 @@ namespace Client.Start
         /// 生成 结果的保存文件名
         /// </summary>
         /// <returns></returns>
-        private string GetReusltFileName()
+        private string GetReusltFileName(string taskId, string revitCmd)
         {
             string localPath = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
-            string fileName = "Result_" + Guid.NewGuid().ToString() + ".txt";
+            // string fileName = "Result_" + Guid.NewGuid().ToString() + ".txt";
+            string fileName = "Result_" + taskId + revitCmd + ".txt";
             return Path.Combine(localPath, "RevitService", fileName);
         }
         /// <summary>

+ 3 - 2
Dispatcher/Client/TaskHandler.cs

@@ -74,12 +74,13 @@ namespace Client
                             try
                             {
                                 HashSet<string> fileSet = new HashSet<string>();
-                                foreach(var downloadModel in task.DownloadTaskModelList) {
+                                foreach (var downloadModel in task.DownloadTaskModelList) {
                                     fileSet.Add(downloadModel.Local_dir);
                                 }
                                 try
                                 {
-                                    string resultJson = revitCommandExcutor.ExecuteCmd(task.Task_cmd, task.Task_param, fileSet);
+                                    
+                                    string resultJson = revitCommandExcutor.ExecuteCmd(task.Task_cmd, task.Task_param, fileSet, task.Id);
                                     simpleHandler.WriteMessage(MessageUtil.generateMessageStr(Command.TaskSuccess.ToString(), task.Id, resultJson));
                                     task.Task_status = 1;
                                     task.Task_result_json = resultJson;

+ 2 - 2
Dispatcher/Client/app.config

@@ -1,8 +1,8 @@
 <?xml version="1.0" encoding="utf-8"?>
 <configuration>
   <appSettings>
-    <add key="IP" value="172.16.42.210" />
-    <!--<add key="IP" value="47.94.89.44" />-->
+    <!--<add key="IP" value="172.16.42.210" />-->
+    <add key="IP" value="47.94.89.44" />
     <add key="Port" value="6666" />
     <add key="DownloadFileDir" value="E:\\" />
     <add key="MaxTaskCount" value="3" />

+ 30 - 6
Starter/StartVisionSelector/Program.cs

@@ -5,6 +5,7 @@ using System.IO;
 using System.Text.RegularExpressions;
 using Newtonsoft.Json.Linq;
 using SAGA.DotNetUtils.Extend;
+using System.Threading;
 
 namespace StartVisionSelector
 {
@@ -107,12 +108,35 @@ namespace StartVisionSelector
 
 
             process.Start();//执行
-            string msg = process.StandardOutput.ReadToEnd();//读取输出
-            Console.WriteLine(msg);
-            process.WaitForExit(60*60*1000);//等待执行完成
-            //process.Close();//结束
-            if(!process.HasExited)
-                process.Kill();
+
+            //process.WaitForExit(60*60*1000);//等待执行完成
+            // process.WaitForExit();
+            try
+            {
+                while (process != null && !process.HasExited)
+                {
+                    process.WaitForExit(60 * 1000); //等待执行完成
+                    if (!process.HasExited && File.Exists(path))
+                    {
+                        Thread.Sleep(5000);
+                        if (process != null && !process.HasExited)
+                        {
+                            process.Close();
+                            process = null;
+                            //process.Kill();
+                            //process.Dispose();
+
+                        }
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                File.AppendAllText(@"d:\error.txt", ex.Message + ex.StackTrace);
+                Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
+            }
+            // string msg = process.StandardOutput.ReadToEnd();//读取输出
+            // Console.WriteLine(msg);
             #endregion
             Console.WriteLine("end");
             //强制退出