locked
process.hasexited always returns true RRS feed

  • Question

  • AudioProcess.StartInfo.FileName = ProgramName;
    AudioProcess =
    new Process();
    AudioProcess.Start();
    bool ProcessRunning = AudioProcess.HasExited;


    My program is automatically opening an audio program when I execute the above code.

    When I set the ProgramName to be the SpeechExec program for a Philips recorder, ProcessRunning always returns true.

    Is Process.HasExited reliable?  How do I find out why it's returning the wrong value?  Is there any other way to check if a program has exited?

    Thank you.

    Tuesday, March 24, 2009 2:11 PM

Answers

  • Your choice of variable name is not the greatest, ProcessTerminated would be more appropriate.  Assuming that's not the real problem, beware that lots of programs behave like a singleton.  They allow only one instance of the program to run. 

    Microsoft Word is perhaps the most common example.  When you start another instance, it simply talks to the first instance and asks it to open the file.  Then immediately exits.  Try it by double-clicking a .doc file a few times.  A speech related program would probably work the same, speech is expensive.  And it doesn't make a lot of sense to start more than one speech recognizer.
    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Monday, March 30, 2009 9:11 AM
    • Unmarked as answer by Melissa S Monday, April 6, 2009 8:43 PM
    • Marked as answer by nobugz Sunday, June 14, 2009 11:01 AM
    Tuesday, March 24, 2009 5:57 PM

All replies

  • You can use the blocking method WaitForExit with the option of specifying a timeout, also I think there is an Exited event?

        var proc = new Process(); 
        var procsi = new ProcessStartInfo(); 
        procsi.FileName = filename; 
        proc.StartInfo = procsi; 
     
        if (proc.Start()) 
        { 
            //process is running 
            proc.WaitForExit(); 
            //process has ended 
        } 

    Tuesday, March 24, 2009 3:49 PM
  • Your choice of variable name is not the greatest, ProcessTerminated would be more appropriate.  Assuming that's not the real problem, beware that lots of programs behave like a singleton.  They allow only one instance of the program to run. 

    Microsoft Word is perhaps the most common example.  When you start another instance, it simply talks to the first instance and asks it to open the file.  Then immediately exits.  Try it by double-clicking a .doc file a few times.  A speech related program would probably work the same, speech is expensive.  And it doesn't make a lot of sense to start more than one speech recognizer.
    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Monday, March 30, 2009 9:11 AM
    • Unmarked as answer by Melissa S Monday, April 6, 2009 8:43 PM
    • Marked as answer by nobugz Sunday, June 14, 2009 11:01 AM
    Tuesday, March 24, 2009 5:57 PM
  • Please add the

    AudioProcess.EnableRaisingEvents = true;


    Hope this helps.

    Nick


    Regards, Nikolay
    Sunday, June 14, 2009 10:16 AM