[英]How to do something while a process is running in Unity3d
I'm running a process from unity and it kinda take some time(it actually can take up to 30 minutes), however I want unity to run it for maximum 5 minutes and if there was no output, return. 我正在统一运行一个过程,大约需要一些时间(实际上可能需要30分钟),但是我希望统一运行它最多5分钟,如果没有输出,请返回。 I also want to show something like this during this wait for 5 minutes time
我也想在等待5分钟的时间内显示类似的内容
anybody has an idea how to do this? 任何人都有一个想法如何做到这一点? I tried using this line of code
我尝试使用此行代码
myProcess.WaitForExit(1000 * 60 * 5);
but I can't do anything while it's waiting, I guess it's blocking me or something, can anybody help? 但是在等待的过程中我什么也做不了,我想这阻碍了我或其他事情,有人可以帮忙吗?
EDITED: 编辑:
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");
}
}
}
}
Do not use myProcess.WaitForExit()
. 不要使用
myProcess.WaitForExit()
。 It will block until it returns. 它将阻塞直到返回。 Use
myProcess.Start()
, then in your Update function, run your animation inside if !myProcess.HasExited
. 使用
myProcess.Start()
,然后在Update函数中, if !myProcess.HasExited
在内部运行动画。 Your code is incomplete so I will provide incomplete solution but this should work. 您的代码不完整,因此我将提供不完整的解决方案,但这应该可以。
void Start()
{
timer = 0;//Reset Timer
myProcess.Start();
}
Check if the process has finished in the Update function 在更新功能中检查过程是否已完成
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
}
}
}
}
Then somewhere else in your callback function where you receive/read from the process, if the bytes read is >0 then always reset timer to 0 . 然后在回调函数中您从进程接收/读取的其他地方,如果读取的字节> 0,则始终将timer重置为0 。 So that the timer will only count to 5 minutes when it has not received anything for 5 minutes .
因此,计时器只有在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);
}
});
OR with a callback function 或使用回调函数
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.