【发布时间】:2016-04-22 08:33:21
【问题描述】:
我正在从 unity 运行一个进程,这需要一些时间(实际上可能需要 30 分钟),但是我希望 unity 运行它最多 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");
}
}
}
}
【问题讨论】:
-
每隔 X 秒做一次定时器检查输出,如果定时器不超过 5 分钟,则停止进程,无需阻塞任何东西
-
@user2320445 WaitForExit 阻止一切。