在 Unity3d 中进程为 运行 时如何做某事

How to do something while a process is running in Unity3d

我正在 运行 从 unity 开始一个过程,这需要一些时间(实际上最多可能需要 30 分钟),但是我希望 unity 运行 它最多 5 分钟如果没有输出,return。 我也想在等待 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");
            }
        }
    }
}

不要使用myProcess.WaitForExit()。它将阻塞直到 returns。使用 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
        }
    }
 }
}

然后在你的回调函数的其他地方你从进程中 receive/read,如果读取的字节是 >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);
   }
 }