Class1.cs
3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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();
}
}
}