HandClientManager.cs 2.7 KB
using HalconDotNet;
using System; 
using System.ComponentModel;
using System.Diagnostics; 
using System.Windows.Forms;
using TSA_V.Common;

namespace TSA_V
{
    public class HandClientManager
    {
        public HandClientManager() { }


        private static Process process = null;
        public static void StartClient()
        {
            //脚本所在地址
            string CurrentPath = System.IO.Directory.GetCurrentDirectory();
            string ScriptFileName = Application.StartupPath + @"\hands\defMTest.py"; ;

            string sArguments = ScriptFileName; //脚本执行文件
            //用于执行程序最后的
            string sep = "-u";

            process = new Process();

            try
            {
                process.StartInfo.FileName = @"python.exe";

                //传递给进程
                process.StartInfo.Arguments = sArguments;
                process.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动
                process.StartInfo.CreateNoWindow = true;   //是否在新窗口中启动该进程的值 (不显示程序窗口)
                process.StartInfo.RedirectStandardInput = true;  // 接受来自调用程序的输入信息
                process.StartInfo.RedirectStandardOutput = true;  // 由调用程序获取输出信息
                process.StartInfo.RedirectStandardError = true;  //重定向标准错误输出

                process.Start();// 启动程序 
                process.BeginOutputReadLine();
                process.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceivedText);

                //process.WaitForExit();  //等待程序执行完退出进程
                //process.Close();
            }

            catch (Win32Exception e)
            {
                LogUtil.error("出错:" + e.ToString());
            }
        }

        public static void pythonDetector4(string[] strArr)
        {
          
        }
        private static void p_OutputDataReceivedText(object sender, DataReceivedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Data))
            { 
                LogUtil.info("pyLog: " + e.Data);
            }
        }

        public static void StopClient()
        {
            try
            {
                if (process != null)
                {
                    if (!process.HasExited)
                    {
                        process.Kill();

                        LogUtil.info($"停止{"defMTest.py"} ");
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("出错:" + ex.ToString());
            }

        }
    }
}