2016-04-22 275 views
0

我正在運行一個統一的進程,需要一些時間(實際上可能需要30分鐘),但是我希望統一運行它最多5分鐘如果沒有輸出,則返回。 我也想在這個等待5分鐘的時間裏展示這樣的東西Wait如何在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"); 
      } 
     } 
    } 
} 
+0

使輸出定時檢查每X秒,而計時器不超過5分鐘,如果它然後停止的過程中,沒有必要阻止任何東西 –

+0

@ user2320445 WaitForExit阻止一切。 – Programmer

回答

2

不要使用myProcess.WaitForExit()。它會阻塞,直到它返回。使用myProcess.Start(),然後在Update功能中,在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然後總是將計時器重置爲。所以定時器只會計數到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); 
    } 
} 
+0

如果你設置'timer = Time.time',然後使用'timer DRKblade

+0

我避開'Time.time'。這是比賽開始的時候。槍越多,數字越大,編號越多。在某些時候,你會得到一個非常大的數字,例如93848589323 + fiveMinutes,沒有人知道它何時重置。 Time.deltaTime返回的數字很小,不需要大數量的操作,它也取決於幀速率,這意味着它將在每個設備中以相同的方式運行。 – Programmer

+0

哥們你是救命恩人!謝謝你,謝謝你,謝謝你 ! – msLangdon95

相關問題