Same code, different results on win2k3 and win2k8 (Process() Class)

Unanswered Same code, different results on win2k3 and win2k8 (Process() Class)

  • Monday, January 05, 2009 5:36 PM
     
      Has Code
    Hello everyone,

    I have made a simple wrapper application to execute 2 applications in order (first it executes one, then it waits for it to finish and then it executes the next one).

    The application works correctly under win2k3 but it starts the applications in parallel in win2k8 (without waiting for the first application to finish before calling the second one).
    1        private void startButton_Click(object sender, EventArgs e) 
    2        { 
    3            ProcessStartInfo psi = new ProcessStartInfo(@"setup.exe"); 
    4            Process myProcess = Process.Start(psi); 
    5            // wait for exit... 
    6            myProcess.WaitForExit(); 
    7            psi = new ProcessStartInfo(@"anothersetup.exe"); 
    8            myProcess = Process.Start(psi); 
    9            myProcess.WaitForExit(); 
    10            MessageBox.Show("Setup finished", "Thank you for using this wrapper!", MessageBoxButtons.OK, MessageBoxIcon.Information); 
    11            Environment.Exit(0); 
    12        } 
    13 


    The problem seems to be related with the myProcess.WaitForExit() method [line 6 and 9] but i really don't understand why it depends on the operating system i'm running it on?

    I haven't recompiled the code or changed anything. Can anybody explain what is happening and, if possible, provide me with a workaround?

    Thanks in advance,
    Luís Silva

All Replies

  • Tuesday, January 06, 2009 3:14 PM
    Moderator
     
     
    There should be no differences since WaitForExit blocks until the process terminates.  Have you confirmed that the first process is running properly under W2K8?  It is possible it is erroring out.  Refer to the output and error streams and the return code to see if there are problems.  I suspect the first process is failing because of security or permission issues.

    Michael Taylor - 1/6/09
    http://p3net.mvps.org
  • Wednesday, January 07, 2009 12:37 PM
     
     
    That's exactly what i was complaining about!

    The code runs on win2k8 BUT it doesn't wait() for the first process() to end!

    I also don't get any exceptions or anything so i'm pretty out of ideas :o(

    Any thoughts?

    Thanks,
    Luís
    • Edited by Luís Miguel Silva Wednesday, January 07, 2009 12:38 PM forgot to ask about "any thoughts"? :oP
    •  
  • Wednesday, January 07, 2009 2:17 PM
    Moderator
     
     
    You won't get any exceptions from the other process even if it crashes.  What is the return code from the first process?  Is there any data in the output or error streams from the first process?  Set a BP after the WaitForExit call on the first process.    When the BP is hit open Task Manager and see if the process is still running?

    Taking a closer look at your process name I'm starting to wonder if the first process is spawning a child process and then terminating.  In this case your code will return pretty quick even though the first process'es child hasn't finished.  If it calls into Windows Installer then this could have been changed between OSes.

    You might also consider setting UseShellExecute to false to make sure the shell isn't getting in the way.  It has to be set in order to fetch the error and output streams anyway.  If that still doesn't work then temporarily try starting a more controllable process like notepad just to confirm the code is working in general.

    Michael Taylor - 1/7/09
    http://p3net.mvps.org
    • Marked As Answer by Zhi-Xin Ye Monday, January 12, 2009 11:30 AM
    • Unmarked As Answer by Luís Miguel Silva Monday, January 12, 2009 1:41 PM
    •  
  • Thursday, January 08, 2009 3:02 PM
     
     
    I'm going to try your suggestions out (specially the UseShellExecute)!

    By the way, the application calls 2 setup applications and they continue to run (although my application doesn't wait for them to finish).

    Like i mentioned, in win2k3 this didn't happen!

    Thanks,
    Luís
  • Monday, January 12, 2009 1:41 PM
     
     
    Dear Tayloer,

    Turning UseShellExecute to false didn't do anything :o(

    I did some further testing and found that psi.HasExited() returns TRUE immediately after the job has been launched!

    Any thoughts?

    Thanks,
    Luís Silva
  • Monday, January 12, 2009 1:57 PM
    Moderator
     
     
    That would tend to mean that the process had no work to do.   What is the exit code from the process?  Did you redirect the output and error streams to see if there were any errors?  Does the process spawn a child process that actually runs to do the work?

    Michael Taylor - 1/12/09
    http://p3net.mvps.org
  • Tuesday, January 13, 2009 10:46 AM
     
     
    Dear Taylor (sorry i misstyped your name yesterday),

    The exitCode associated with myProcess is 0 :o\

    I noticed another VERY ODD thing, the code works as expected when i start it from visual studio...

    And then i found another thing, although i'm calling "the same setup applications", the ones i'm using inside visual studio were compiled from visual studio 2005 (and are from an earlier version of my product).

    So i'm guessing this is related to the way visual studio 2008 makes it's setup (maybe it's spawning a child just like you said...)

    Is there a way to fix this? Any thoughts?


    Thanks,
    Luís


  • Tuesday, January 13, 2009 2:14 PM
    Moderator
     
     

    Oh hang on a minute.  Do you have UAC enabled under Server 2008?  If so then the call is probably being failed by UAC since it requires admin privileges.  Note that even if you are running as an admin user you still don't have admin privileges until you elevate the process.  This would explain the immediate return.  And yet you are likely an admin when running under the debugger so it would work. 

    You can confirm that your code is working otherwise by starting something like notepad instead just to make sure it is specifically related to the setup process rather than your code.

    Michael Taylor - 1/13/09
    http://p3net.mvps.org

  • Monday, January 19, 2009 11:02 AM
     
     
    Well, this is a default install for win2k8 HPC Server (which i think is based on Win2k8 Server) but the same happens even if i run the application in Windows XP!

    I tried running the application as an administrator (just like you said) but had no luck :o(

    Thanks,
    Luís
  • Monday, January 19, 2009 2:22 PM
    Moderator
     
     

    So your saying that it fails under W2K8 and XP but it works under W2K3?

    Did you try the notepad test? 
    Did you try setting a breakpoint at the WaitForExit call, running your code and waiting for the debugger to break and then confirming that the process is still running in the background? 

    Could you post the rest of the ProcessStartInfo properties you've set.  I'm still almost 100% confident it is the process you're trying to start rather than the framework.  It has all the symptoms of a process that it exiting too quickly.

    Michael Taylor - 1/19/09
    http://p3net.mvps.org

  • Thursday, January 22, 2009 6:16 PM
     
     
    Dear Taylor,

    Sorry for the delay in my reply!

    My initial statement was made under the assumption that it worked on win2k3 (because it did..in the past)!

    Like i mentioned, i have concluded the problem IS in fact related to the process i'm running (in this case, the setup packages generated by win2k8)!

    I tried running a command prompt and, from what i recall, it worked correctly!

    So, now i need to figure how to make it stop behaving like that :o)
    The other solution (even better) would be for me to learn how to make a setup package for both the web service and the windows services!

    I have 3 applications:
    - two windows services
    - one web service

    I didn't find any way to create a setup package for the entire solution so i simply created one for the windows services and another one for the web service.

    Is there any way to bundle them all together? I could only find one type of setup project for each of my applications and not a global solution that could install the entire project.

    Thank you,
    Luís Silva