I used the soultion from here:
ProcessStartInfo hanging on "WaitForExit"? Why?
To create a run process and get it's StandardOutput and StandardError.
The problem is, that is still hangs!
I believe it may be because I am using cmd.
The "noJava" in my code is actually a Filepath to a .class file, which prints out a command and sends it to a unity server.
if I do the call manually java -cp E:/Java/src/ Lift
in the cmd, I first receive 1 line of code: Command: objects["Door"].transform.position.x += ((new Vector3(0.0,1.0,0.0))).x*0.6;objects["Door"].transform.position.y += ((new Vector3(0.0,1.0,0.0))).y*0.6;objects["Door"].transform.position.z += ((new Vector3(0.0,1.0,0.0))).z*0.6;
and only then, if I switch the window to unity, I receive the rest of the message (I believe the server is only accepting connections while unity is the active window).
Now if I try doing it this way it hangs:
using (run_process = new Process())
{
run_process.StartInfo.FileName = "cmd";
run_process.StartInfo.Arguments = " /C java -cp "+noJava;
//run_process.StartInfo.CreateNoWindow = true;
run_process.StartInfo.UseShellExecute = false;
run_process.StartInfo.RedirectStandardOutput = true;
run_process.StartInfo.RedirectStandardError = true;
StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();
using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
{
run_process.OutputDataReceived += (sender, e) => {
if (e.Data == null)
{
outputWaitHandle.Set();
}
else
{
output.AppendLine(e.Data);
}
};
run_process.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null)
{
errorWaitHandle.Set();
}
else
{
error.AppendLine(e.Data);
}
};
run_process.Start();
run_process.BeginOutputReadLine();
run_process.BeginErrorReadLine();
if (run_process.WaitForExit(3000) &&
outputWaitHandle.WaitOne(3000) &&
errorWaitHandle.WaitOne(3000))
{
// Process completed. Check process.ExitCode here.
}
else
{
// Timed out.
}
}
}
I have tried to solve this problem for 2 days now and still was not able to make it work. Please help!