我正在運行一個統一的進程,需要一些時間(實際上可能需要30分鐘),但是我希望統一運行它最多5分鐘如果沒有輸出,則返回。 我也想在這個等待5分鐘的時間裏展示這樣的東西如何在Unity3d中運行進程時執行某些操作
任何人都有一個想法如何做到這一點?我試着用這行代碼
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阻止一切。 – Programmer