Process.WaitForExit Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stoogots2
    New Member
    • Sep 2007
    • 77

    Process.WaitForExit Question

    I have written a windows application that kicks off another process and then Waitsforexit. I have one old crappy machine on which this works great and has been for months and in the last couple of months I've seen that on a newer faster machine that it doesn't seem to work reliably. Both are XP SP3 and the app utilizes Framework 2.0. Any ideas?

    Code:
    ProcessStartInto ps=new ProcessStartInfo();
    ps.FileName=sFilename;
    ps.Arguments=sArguments;
    ps.WorkingDirectory=sWorkingDirectory;
    p.EnableRaisingEvents=true;
    
    p=Process.Start(ps);
    try
    {
    p.WaitForExit();
    TaskStopped(p);
    p.Close();
    }
    catch (Exception ex)
    {
    //Log Error Message only
    }
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    What do you mean doesn't work reliably?
    Is the process starting and ending faster then you can follow on the newer machine perhaps?

    Comment

    • stoogots2
      New Member
      • Sep 2007
      • 77

      #3
      Thanks for the response Plater. The process usually takes about 90 minutes to complete but another thread checks every minute to make sure it takes no longer than 180 minutes or it kills it. I saw in the log that the process got killed after 180 minutes although the output that was expected from the process was completed in the normal timeframe. I've seen this more than once, but only on this one computer. I have 3 other computers running the same code without ever having experienced this problem.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        So its not the waitforexit() that's failing, its that the process is never exiting.

        Comment

        • stoogots2
          New Member
          • Sep 2007
          • 77

          #5
          I have every indication that the process is exiting except that I do not see the WaitForExit completing and the paused thread continuing. The output of the process is there and in all other cases where this code is executing, the process has exited. There shouldn't be anything different about the 5th machine running the same application/framework/operating system/service pack.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            I thought you said you had a seperate thread that is checking to make sure the processes ended and kills it if it didn't. If you can still see the process in that thread, it had never ended

            Comment

            • stoogots2
              New Member
              • Sep 2007
              • 77

              #7
              The main thread just waits a configurable amount of time and Process.Kills the worker thread if it doesn't proceed past p.WaitForExit() .

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Hmm is there any reason it NEEDS to wait for exit? If it just gets killed off anyway?

                Comment

                • stoogots2
                  New Member
                  • Sep 2007
                  • 77

                  #9
                  Yes, there is a need for it to waitforexit. It only gets killed off if there is not a problem, so in most cases it won't get killed. I kill the process only when it has run for twice the "normal" completion period without completing. The problem is that if there isn't a problem, it doesn't seem to return from WaitForExit every time. I have 3 other instances of the same program running and returning from WaitForExit every time. It is just strange that the new computers don't return each and every time.

                  Comment

                  • GaryTexmo
                    Recognized Expert Top Contributor
                    • Jul 2009
                    • 1501

                    #10
                    Out of curiosity, have you tried using the event handler on the process object? I'm not saying you should use it instead, I'm just curious of the callback works for you, whereas the waitforexit seems to be behaving oddly.

                    Code:
                                    Process myProcess = new Process();
                                    myProcess.StartInfo.FileName = ...;
                                    myProcess.StartInfo.Arguments = ...;
                                    myProcess.EnableRaisingEvents = true;
                                    myProcess.Exited += new EventHandler(mergeToolProcess_Exited);
                    
                                    myProcess.Start();

                    Comment

                    Working...