0

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!

Community
  • 1
  • 1
Setup
  • 330
  • 2
  • 21
  • unity is paused when it isn't the active window, try it with a built app – CodeSmile Jul 12 '14 at 20:00
  • @LearnCocos2D Even with a build app, it does not work... Any other ideas? – Setup Jul 12 '14 at 21:53
  • For anyone else who is having the same problem: If finally found a solution. The problem is, that unity starts adb.exe when entering playmode, which interrepted the server from being shut down properly. Link to the bug: http://forum.unity3d.com/threads/bug-unity-runs-adb-exe-when-entering-play-mode-in-the-editor.148176/ – Setup Aug 01 '14 at 10:09

1 Answers1

0

For anyone else who is having the same problem: If finally found a solution.

The problem is, that unity starts adb.exe when entering playmode, which interrupted the server from being shut down properly. Link to the bug: http://forum.unity3d.com/threads/bug-unity-runs-adb-exe-when-entering-play-mode-in-the-editor.148176/

After writing a small "adb.exe" process killer, everything started to work fine! :D

Setup
  • 330
  • 2
  • 21