C# process 执行exe文件 死循环问题
我用tcc命令编译生成xx.c文件在执行时若.c文件里面有死循环如何结束?如何获取其运行时间?代码如下stringfileName=this.compiler+probl...
我用tcc命令编译生成xx.c文件 在执行时若.c文件里面有死循环如何结束?
如何获取其运行时间?代码如下
string fileName = this.compiler + problemId + userName + ".exe";
StreamReader sr = File.OpenText(this.judgeIn + problemId + ".in");
string test = sr.ReadToEnd();
sr.Close();
this.p.StartInfo.CreateNoWindow = false;
this.p.StartInfo.UseShellExecute = false;
this.p.StartInfo.FileName = fileName;
this.p.StartInfo.RedirectStandardError = true;
this.p.StartInfo.RedirectStandardInput = true;
this.p.StartInfo.RedirectStandardOutput = true;
Timer timer = new Timer();
timer.Interval = Convert.ToDouble(time);//设置timer间隔
timer.Enabled = true;
timer.AutoReset = false;
this.p.Start();
timer.Start();
this.p.StandardInput.Write(test + '\n');
// string message = this.process.StandardError.ReadToEnd();
string output = this.p.StandardOutput.ReadToEnd();
this.p.WaitForExit();
TimeSpan ts = new TimeSpan();
ts = this.p.ExitTime - this.p.StartTime;//此处报错 句柄无效
this.p.Close();
timer.Stop();
return output; 展开
如何获取其运行时间?代码如下
string fileName = this.compiler + problemId + userName + ".exe";
StreamReader sr = File.OpenText(this.judgeIn + problemId + ".in");
string test = sr.ReadToEnd();
sr.Close();
this.p.StartInfo.CreateNoWindow = false;
this.p.StartInfo.UseShellExecute = false;
this.p.StartInfo.FileName = fileName;
this.p.StartInfo.RedirectStandardError = true;
this.p.StartInfo.RedirectStandardInput = true;
this.p.StartInfo.RedirectStandardOutput = true;
Timer timer = new Timer();
timer.Interval = Convert.ToDouble(time);//设置timer间隔
timer.Enabled = true;
timer.AutoReset = false;
this.p.Start();
timer.Start();
this.p.StandardInput.Write(test + '\n');
// string message = this.process.StandardError.ReadToEnd();
string output = this.p.StandardOutput.ReadToEnd();
this.p.WaitForExit();
TimeSpan ts = new TimeSpan();
ts = this.p.ExitTime - this.p.StartTime;//此处报错 句柄无效
this.p.Close();
timer.Stop();
return output; 展开
1个回答
展开全部
这样改:
string fileName = this.compiler + problemId + userName + ".exe";
StreamReader sr = File.OpenText(this.judgeIn + problemId + ".in");
string test = sr.ReadToEnd();
sr.Close();
DateTime startt = DateTime.Now;
this.p.StartInfo.CreateNoWindow = false;
this.p.StartInfo.UseShellExecute = false;
this.p.StartInfo.FileName = fileName;
this.p.StartInfo.RedirectStandardError = true;
this.p.StartInfo.RedirectStandardInput = true;
this.p.StartInfo.RedirectStandardOutput = true;
Timer timer = new Timer();
timer.Interval = Convert.ToDouble(time);//设置timer间隔
timer.Enabled = true;
timer.AutoReset = false;
this.p.Start();
timer.Start();
this.p.StandardInput.Write(test + '\n');
// string message = this.process.StandardError.ReadToEnd();
string output = this.p.StandardOutput.ReadToEnd();
this.p.WaitForExit();
TimeSpan ts = new TimeSpan();
ts = startt - DateTime.Now;//此处报错 句柄无效
this.p.Close();
timer.Stop();
return output;
string fileName = this.compiler + problemId + userName + ".exe";
StreamReader sr = File.OpenText(this.judgeIn + problemId + ".in");
string test = sr.ReadToEnd();
sr.Close();
DateTime startt = DateTime.Now;
this.p.StartInfo.CreateNoWindow = false;
this.p.StartInfo.UseShellExecute = false;
this.p.StartInfo.FileName = fileName;
this.p.StartInfo.RedirectStandardError = true;
this.p.StartInfo.RedirectStandardInput = true;
this.p.StartInfo.RedirectStandardOutput = true;
Timer timer = new Timer();
timer.Interval = Convert.ToDouble(time);//设置timer间隔
timer.Enabled = true;
timer.AutoReset = false;
this.p.Start();
timer.Start();
this.p.StandardInput.Write(test + '\n');
// string message = this.process.StandardError.ReadToEnd();
string output = this.p.StandardOutput.ReadToEnd();
this.p.WaitForExit();
TimeSpan ts = new TimeSpan();
ts = startt - DateTime.Now;//此处报错 句柄无效
this.p.Close();
timer.Stop();
return output;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询