Class1.cs 3.4 KB
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Asa
{
    public class CountImage
    {
        private string _path1, _path2, _path3;
        private string _count;
        private readonly log4net.ILog LOG;

        public CountImage(string logName = "CountImage")
        {
            LOG = log4net.LogManager.GetLogger(logName);
        }

        public void SetDirectory(string path1, string path2, string path3)
        {
            _path1 = path1;
            _path2 = path2;
            _path3 = path3;
        }

        public bool GetCount(int threshold, out int count)
        {
            Thread tRun = new Thread(new ParameterizedThreadStart(RunCmd));
            tRun.Start(threshold);

            do
            {
                Debug.Print(tRun.ThreadState.ToString());
                Thread.Sleep(50);
                System.Windows.Forms.Application.DoEvents();
            }
            while (tRun.ThreadState == System.Threading.ThreadState.Running);

            count = Convert.ToInt32(Convert.ToSingle(_count));
            return true;
        }

        private void RunCmd(object obj)
        {
            int th = (int)obj;
            if (th <= 0 || th >= 100) th = 30;

            _count = "0";
            Process p = new Process();
            string s1 = Environment.CurrentDirectory + "\\neotel_count\\neotel_count.exe";
            if (System.IO.File.Exists(s1))
                p.StartInfo.FileName = s1;
            else
                return;

            //p.StartInfo.FileName = Environment.CurrentDirectory+ "\\count\\count_nocolor.exe";
            p.StartInfo.Arguments = string.Format("{0} {1} {2} {3}", _path1, _path2, _path3, th);
            p.StartInfo.UseShellExecute = false;        //是否使用操作系统shell启动
            p.StartInfo.RedirectStandardInput = true;   //接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardOutput = true;  //由调用程序获取输出信息
            p.StartInfo.RedirectStandardError = true;   //重定向标准错误输出
            p.StartInfo.CreateNoWindow = true;          //不显示程序窗口
            p.Start();

            //向cmd窗口发送输入信息
            //p.StandardInput.WriteLine();
            //p.StandardInput.AutoFlush = true;

            //while(true)
            //{
            //    string s = p.StandardOutput.ReadLine();
            //    Debug.Print(s);

            //    if (s.Contains("总数量:"))
            //        _count = s.Replace("总数量:", "");
            //    else if (s.Contains("点料完成"))
            //        break;
            //    else if (s == null)
            //        break;

            //    Thread.Sleep(10);
            //}

            string text = p.StandardOutput.ReadToEnd();
            Debug.Print(text);
            LOG.Info(text);
            string[] arr = text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            string s = "总数量:";
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i].Contains(s))
                {
                    _count = arr[i].Replace(s, "");
                    break;
                }
            }

            p.WaitForExit();
            p.Close();
            //p.Kill();
        }


    }
}