正常来说,那么winform是获取不到EXE的返回值,这时我所用的方法是把EXE中的值写到csv文件中,然后winform去读取csv
private void btnGoodsSelect_Click(object sender, EventArgs e)
{
ParameterizedThreadStart pts = new ParameterizedThreadStart(RunCommand);
Thread FirstChildThread = new Thread(pts);
FirstChildThread.Start("Apsi.exe H111601010102I.psp +ARG -B " + basesCode + " -G " + txtGoods.Text.ToString() + " -S " + goodsSegment + "");
}
/// <summary>
/// 执行Commond命令
/// </summary>
/// <param name="CommandName">CommandName</param>
public void RunCommand(object CommandName)
{
if (process == null)
{
process = new Process();
process.StartInfo.FileName = "cmd.exe"; //确定程序名
process.StartInfo.Arguments = "/c " + CommandName; //确定程式命令行
process.StartInfo.UseShellExecute = false; //Shell的使用
process.StartInfo.RedirectStandardInput = true; //重定向输入
process.StartInfo.RedirectStandardOutput = true; //重定向输出
process.StartInfo.RedirectStandardError = true; //重定向输出错误
process.StartInfo.CreateNoWindow = true; //设置不显示示窗口
process.Exited += new EventHandler(p_Exited);//在process结束时调用
process.EnableRaisingEvents = true;
process.Start();
process.WaitForExit();
process.Close();
process.Dispose();
process = null;
}
}
/// <summary>
/// 自定义进程退出事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void p_Exited(object sender, EventArgs e)
{
//创建委托,InvokeFun时它的托管代码
MethodInvoker mi = new MethodInvoker(this.InvokeFun);
this.BeginInvoke(mi);//异步执行委托;
}
/// <summary>
/// 委托mi的托管代码
/// </summary>
private void InvokeFun()
{
StreamReader sr = new StreamReader("C:\\Test.csv", Encoding.Default);
string s = "";
while ((s = sr.ReadLine()) != null)
{
string[] str = s.Split(',');
this.txtGoods.Text = str[0].Replace("\"", "");
this.goodsSegment = str[2].Replace("\"", "");
}
sr.Close();
GoodsCheck();
this.txtGoods.Focus();
this.txtGoods.SelectionStart = 0;
}