先贴代码 /// <summary>
/// 执行 宽带 挂断 与连接
/// </summary>
/// <param name="cmds"></param>
private void cmdExecute(List<String> cmds)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
//p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true; p.EnableRaisingEvents = true;
p.Exited += new EventHandler(p_Exited);//线程退出
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);//异步监听输出信息
//p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);//异步监听错误信息
//try{
p.Start();
for (Int32 i = 0; i < cmds.Count; i++)
{
p.StandardInput.WriteLine(cmds[i]);
}
p.BeginOutputReadLine();
//p.BeginErrorReadLine();
//String error = p.StandardError.ReadToEnd();//标记1 : 这里必然要出错
p.StandardInput.WriteLine("exit");
p.WaitForExit();
p.Close();
//}
//catch{}
}
/// <summary>
/// 委托.. 利用这个操作主线程中的控件
/// </summary>
public delegate void OutputDataReceived(Object sender, DataReceivedEventArgs e);
void p_OutputDataReceived(Object sender, DataReceivedEventArgs e)
{
try
{
if (this.InvokeRequired)
{
OutputDataReceived o = new OutputDataReceived(p_OutputDataReceived);
Invoke(o, new Object[] { sender, e });//标记2
}
else
{
richTextBox1.AppendText(e.Data);
richTextBox1.AppendText("\n");
}
}
catch (Exception mes)
{
MessageBox.Show(mes.ToString()+"//线程异常");
}
}问题就出在上面这 段里面.. 如果 把cmdExecute中的 try 打开 吧标记1位置注释打开 不处理错误
主线程里的控件 richTextBox1能正常显示返回的信息.. 界面也不会卡住..
这当然是不允许的.. 不能同时 异步和同步操作StandardOutput..
如果就上面这段代码执行. 能执行完 但是主线程一直在p.WaitForExit(); 求高手救命啊.. 刚开始winform 什么都不懂...
/// 执行 宽带 挂断 与连接
/// </summary>
/// <param name="cmds"></param>
private void cmdExecute(List<String> cmds)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
//p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true; p.EnableRaisingEvents = true;
p.Exited += new EventHandler(p_Exited);//线程退出
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);//异步监听输出信息
//p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);//异步监听错误信息
//try{
p.Start();
for (Int32 i = 0; i < cmds.Count; i++)
{
p.StandardInput.WriteLine(cmds[i]);
}
p.BeginOutputReadLine();
//p.BeginErrorReadLine();
//String error = p.StandardError.ReadToEnd();//标记1 : 这里必然要出错
p.StandardInput.WriteLine("exit");
p.WaitForExit();
p.Close();
//}
//catch{}
}
/// <summary>
/// 委托.. 利用这个操作主线程中的控件
/// </summary>
public delegate void OutputDataReceived(Object sender, DataReceivedEventArgs e);
void p_OutputDataReceived(Object sender, DataReceivedEventArgs e)
{
try
{
if (this.InvokeRequired)
{
OutputDataReceived o = new OutputDataReceived(p_OutputDataReceived);
Invoke(o, new Object[] { sender, e });//标记2
}
else
{
richTextBox1.AppendText(e.Data);
richTextBox1.AppendText("\n");
}
}
catch (Exception mes)
{
MessageBox.Show(mes.ToString()+"//线程异常");
}
}问题就出在上面这 段里面.. 如果 把cmdExecute中的 try 打开 吧标记1位置注释打开 不处理错误
主线程里的控件 richTextBox1能正常显示返回的信息.. 界面也不会卡住..
这当然是不允许的.. 不能同时 异步和同步操作StandardOutput..
如果就上面这段代码执行. 能执行完 但是主线程一直在p.WaitForExit(); 求高手救命啊.. 刚开始winform 什么都不懂...
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货