【发布时间】:2014-08-28 08:52:21
【问题描述】:
我想通过 c# 运行 Iperf 通过 cmd 运行时,一切正常且快速 但我使用此代码通过 c# 运行它:
public void RunProcess(string FileName, string Arguments, bool EventWhenExit )
{
process = new Process();
process.EnableRaisingEvents = true;
process.OutputDataReceived += new DataReceivedEventHandler(OnDataReceivedEvent);
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.LoadUserProfile = false;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = FileName; // Gets or sets the application or document to start.
process.StartInfo.Arguments =Arguments;//Gets or sets the set of command-line arguments to use when starting the application
Thread.Sleep(1000);
if (EventWhenExit)
{
process.EnableRaisingEvents = true;
process.Exited += new EventHandler(myprocess_Exited);/*New line */
}
process.Start();
process.BeginOutputReadLine();
PID = process.Id;
}
private void myprocess_Exited(object sender, EventArgs e)
{
process.Refresh();
Thread.Sleep(2000);
onProcessEnd(this, "ENDOF " + Proc.ToString());
Console.WriteLine("Process exsiting ");
}
private void OnDataReceivedEvent(object sender, DataReceivedEventArgs e)
{
string OutputFromProcess = e.Data;
//fire event to event handler class for further use
onDataOutputFromProcess(this, OutputFromProcess, Proc.ToString());
}
我得到了错误的奇怪行为:
当运行 1 个流时(那些使用 Iperf 的人会知道...)在控制台和我的应用程序中都可以正常工作(winform)
但是我运行 3 个及以上的流,我的应用程序不会结束,而不仅仅是在它应该退出时挂起
可能是什么问题? 有什么好的方法可以解决这个问题?
【问题讨论】:
-
对于不起作用的代码,请创建MVCE,以便我们为您提供帮助。
-
我会继续努力的 :) 同时你能告诉我我在代码中设置流程的方式是否有问题吗?
标签: c# process console-application