Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomatically starting a java-web-start application from C# and .NET, .WaitForExit(); doesn't work as desired
    text
    copied!<p>I'm using a C# application to launch a java web application. I'd like my C# application to know when the user closes out of the java app. I'm using this code:</p> <pre><code>var javaws = File.Exists(@"C:\Program Files\Java\jre6\bin\javaws.exe") ? @"C:\Program Files\Java\jre6\bin\javaws.exe" : @"C:\Program Files (x86)\Java\jre6\bin\javaws.exe"; var psi = new ProcessStartInfo(javaws, String.Format("http://{0}:/appstart.jnlp", hostAddress)); Process.Start(psi).WaitForExit(); </code></pre> <p>This code successfully launches the java program, however, <code>WaitForExit()</code> is called and immediately returns. I believe this is because "javaws" simply starts another process called "javaw" and then "javaws" closes. Is there any way to A) wait for all child processes to finish too, or B) wait for a specific child process?</p> <p>--</p> <p>I've discovered this related post: <a href="https://stackoverflow.com/questions/3803823/are-javaws-exit-codes-really-broken">Are javaws exit codes really broken?</a>, though the answer there suggests that 1.6.0_23 has fixed it, I am seeing the exact same behavior on my development machine with Java 1.6.0_23. What I am looking for now is a workaround to make the above code work as expected. I need to start this jnlp file, wait for its execution to complete, and then do some additional code in C# program. The Java application is out of my control, so I can't add the functionality there.</p> <p>--</p> <p>For anyone wondering, the final solution looks like this:</p> <pre><code>var javaws = File.Exists(@"C:\Program Files\Java\jre6\bin\javaws.exe") ? @"C:\Program Files\Java\jre6\bin\javaws.exe" : @"C:\Program Files (x86)\Java\jre6\bin\javaws.exe"; var psi = new ProcessStartInfo(javaws, String.Format("http://{0}:/appstart.jnlp", hostAddress)); Process.Start(psi).WaitForExit(); var javaw = Process.GetProcessesByName("javaw"); javaw.Single(ja =&gt; ja.StartTime.Equals(javaw.Max(j =&gt; j.StartTime))).WaitForExit(); </code></pre> <p>This provides the added benefit of only waiting for the most recently started <code>javaw</code> process, in the event there are other java web apps running on the machine.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload