我正在运行一个统一的过程,它需要一些时间(实际上可能需要长达30分钟),但我希望团结运行最多5分钟,如果没有输出,则返回。 我也希望在等待5分钟
期间显示类似的内容任何人都知道如何做到这一点?我尝试使用这行代码
myProcess.WaitForExit(1000 * 60 * 5);
但在等待的时候我什么也做不了,我想这会阻止我或其他什么,有人可以帮忙吗?
已编辑:
public void onClickFindSol(){
paused=true;
ReadRedFace();
ReadGreenFace();
ReadBlueFace();
ReadYellowFace();
ReadOrangeFace();
ReadWhiteFace();
if (File.Exists (path))
File.Delete (path);
System.IO.File.WriteAllText(path,InputToAlgo);
myProcess = new Process();
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.FileName = (System.Environment.CurrentDirectory )+Path.DirectorySeparatorChar+"rubik3Sticker.ida2";
myProcess.EnableRaisingEvents = true;
myProcess.StartInfo.WorkingDirectory = (System.Environment.CurrentDirectory )+Path.DirectorySeparatorChar;
myProcess.StartInfo.Arguments = "corner.bin edge1.bin edge2.bin";
myProcess.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data)){
timer = 0f;
StepsOfSolution++;
print(e.Data);
solution.output.Add(e.Data);
}
});
myProcess.Start();
myProcess.BeginOutputReadLine();
}
void Update(){
if (myProcess != null){
if(timer>fiveMinutes){
myProcess.Kill();
myProcess.Close();
badExit=true;
myProcess=null;
return;
}
timer += Time.deltaTime;
if (!myProcess.HasExited){
RubikScene.PleaseWait.SetActive(true);
}
else{
if(badExit){
RubikScene.PleaseWait.SetActive(false);
RubikScene.TooLong.SetActive(true);
print("TimeOut!");
}else{
paused=false;
Application.LoadLevel("solution");
}
}
}
}
答案 0 :(得分:2)
请勿使用myProcess.WaitForExit()
。它会阻塞直到它返回。使用myProcess.Start()
,然后在更新功能中,在if !myProcess.HasExited
内运行动画。你的代码不完整,所以我会提供不完整的解决方案,但这应该有用。
void Start()
{
timer = 0;//Reset Timer
myProcess.Start();
}
检查更新功能
中的流程是否已完成float timer = 0f;
float fiveMinutes = 300; //300 seconds = 5minutes
bool badExit = false;
void Update()
{
if (myProcess != null)
{
//Check if Time has reached
if(timer>fiveMinutes){
myProcess.Kill();
myProcess.Close();
badExit = true;
return;
}
timer += Time.deltaTime;
if (!myProcess.HasExited)
{
//Do Your Animation Stuff Here
}else{
//Check if this was bad or good exit
if(badExit){
//Bad
}else{
//Good
}
}
}
}
然后在回调函数中的其他位置接收/读取进程,如果读取的字节为> 0 ,则始终将计时器重置为 0 。因此,当 5分钟没有收到任何内容时,计时器只会计入 5分钟。
myProcess.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
// Prepend line numbers to each line of the output.
if (!String.IsNullOrEmpty(e.Data))
{
timer = 0f; //Reset Process Timer
lineCount++;
output.Append("\n[" + lineCount + "]: " + e.Data);
}
});
或回调功能
private static void SortOutputHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
// Collect the sort command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
timer = 0f; //Reset Process Timer
numOutputLines++;
// Add the text to the collected output.
sortOutput.Append(Environment.NewLine +
"[" + numOutputLines.ToString() + "] - " + outLine.Data);
}
}