Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with HasExited and WaitForExit()

Problem with HasExited and WaitForExit()

Scheduled Pinned Locked Moved C#
helpannouncement
30 Posts 10 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D danielhasdibs

    I've asked this before, but I didn't get an answer that fixed the problem. Here is what I want to do: I have a program that opens two applications (a Word document, and a hyperlink). It opens one after the other. What I'm trying to do is open the first application (the Word document) and AFTER the user closes the application, open the second one (the hyperlink). I have tried using the boolean HasExited as well as the method WaitForExit(). Neither are working. As soon as the first application opens, the HasExited is false and WaitForExit() does nothing, even when I use the overloaded version. Here is my code: System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; psi.UseShellExecute = false; psi.Arguments = @txt_Wordfile.Text; psi.FileName = @WordExe[0]; System.Diagnostics.Process WordProcess = new System.Diagnostics.Process(); WordProcess = System.Diagnostics.Process.Start(psi); WordProcess.EnableRaisingEvents = true; WordProcess.WaitForExit(10000); System.Diagnostics.Process.Start(txt_websiteAddress.Text); Please Help! Thanks!

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #21

    Hi, this code works fine on WinXP:

    Process proc=Process.Start(@"C:\\test.doc");
    log("procName="+proc.ProcessName);
    proc.WaitForExit();
    log("has exited");
    

    provided no Word process is running yet (if there is, proc is null as per MSDN documentation). PS: I do not use any Office Assistant, I have thrown it out long ago. IIRC there is a small app that "helps" in launching Office components, if that were to intercept the doc extension and then launch WinWord on its own, you would not get winword in procName; you could fix that by explicitly starting winword, with the doc name as an argument tho). :)

    Luc Pattyn [Forum Guidelines] [My Articles]


    This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


    D 1 Reply Last reply
    0
    • L Luc Pattyn

      Hi, this code works fine on WinXP:

      Process proc=Process.Start(@"C:\\test.doc");
      log("procName="+proc.ProcessName);
      proc.WaitForExit();
      log("has exited");
      

      provided no Word process is running yet (if there is, proc is null as per MSDN documentation). PS: I do not use any Office Assistant, I have thrown it out long ago. IIRC there is a small app that "helps" in launching Office components, if that were to intercept the doc extension and then launch WinWord on its own, you would not get winword in procName; you could fix that by explicitly starting winword, with the doc name as an argument tho). :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #22

      Process.WaitForExit(); and Process.StandardOutput.ReadToEnd(); seem to have the same result. Any idea why the Exited event isn't fired Luc?

      Dave

      modified on Friday, February 8, 2008 5:44 PM

      L 1 Reply Last reply
      0
      • D DaveyM69

        Process.WaitForExit(); and Process.StandardOutput.ReadToEnd(); seem to have the same result. Any idea why the Exited event isn't fired Luc?

        Dave

        modified on Friday, February 8, 2008 5:44 PM

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #23

        :confused: ReadToEnd() did not appear in the original post, nor in my only reply :confused:

        Luc Pattyn [Forum Guidelines] [My Articles]


        This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


        D 1 Reply Last reply
        0
        • L Luc Pattyn

          :confused: ReadToEnd() did not appear in the original post, nor in my only reply :confused:

          Luc Pattyn [Forum Guidelines] [My Articles]


          This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #24

          No - I was just comenting that it appears to have the same result

          Dave

          1 Reply Last reply
          0
          • D danielhasdibs

            I've asked this before, but I didn't get an answer that fixed the problem. Here is what I want to do: I have a program that opens two applications (a Word document, and a hyperlink). It opens one after the other. What I'm trying to do is open the first application (the Word document) and AFTER the user closes the application, open the second one (the hyperlink). I have tried using the boolean HasExited as well as the method WaitForExit(). Neither are working. As soon as the first application opens, the HasExited is false and WaitForExit() does nothing, even when I use the overloaded version. Here is my code: System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; psi.UseShellExecute = false; psi.Arguments = @txt_Wordfile.Text; psi.FileName = @WordExe[0]; System.Diagnostics.Process WordProcess = new System.Diagnostics.Process(); WordProcess = System.Diagnostics.Process.Start(psi); WordProcess.EnableRaisingEvents = true; WordProcess.WaitForExit(10000); System.Diagnostics.Process.Start(txt_websiteAddress.Text); Please Help! Thanks!

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #25

            danielhasdibs wrote:

            WaitForExit() does nothing

            Nothing ever does "nothing". What does it do? Return immediately?

            danielhasdibs wrote:

            System.Diagnostics.Process WordProcess = new System.Diagnostics.Process();

            Why are you creating a new process here? You only throw it away in the next line where you create another new process.

            danielhasdibs wrote:

            WordProcess = System.Diagnostics.Process.Start(psi);

            You should check the return value. If a process is reused instead or starting a new, the return value is null.

            Experience is the sum of all the mistakes you have done.

            1 Reply Last reply
            0
            • D danielhasdibs

              I've asked this before, but I didn't get an answer that fixed the problem. Here is what I want to do: I have a program that opens two applications (a Word document, and a hyperlink). It opens one after the other. What I'm trying to do is open the first application (the Word document) and AFTER the user closes the application, open the second one (the hyperlink). I have tried using the boolean HasExited as well as the method WaitForExit(). Neither are working. As soon as the first application opens, the HasExited is false and WaitForExit() does nothing, even when I use the overloaded version. Here is my code: System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; psi.UseShellExecute = false; psi.Arguments = @txt_Wordfile.Text; psi.FileName = @WordExe[0]; System.Diagnostics.Process WordProcess = new System.Diagnostics.Process(); WordProcess = System.Diagnostics.Process.Start(psi); WordProcess.EnableRaisingEvents = true; WordProcess.WaitForExit(10000); System.Diagnostics.Process.Start(txt_websiteAddress.Text); Please Help! Thanks!

              E Offline
              E Offline
              engsrini
              wrote on last edited by
              #26

              Hi Why did you give the wait for exit time as 10000 i.e WordProcess.WaitForExit(10000); if you want the first application to be closed and open the second one do like this WordProcess.WaitForExit();// wait for infinite System.Diagnostics.Process.Start(txt_websiteAddress.Text); And about HasExited property, if any of the word document is open other than the closed document, this property will not get updated. Since Word is single process which launch different instance of editor. Thanks Srini

              1 Reply Last reply
              0
              • D danielhasdibs

                I've asked this before, but I didn't get an answer that fixed the problem. Here is what I want to do: I have a program that opens two applications (a Word document, and a hyperlink). It opens one after the other. What I'm trying to do is open the first application (the Word document) and AFTER the user closes the application, open the second one (the hyperlink). I have tried using the boolean HasExited as well as the method WaitForExit(). Neither are working. As soon as the first application opens, the HasExited is false and WaitForExit() does nothing, even when I use the overloaded version. Here is my code: System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; psi.UseShellExecute = false; psi.Arguments = @txt_Wordfile.Text; psi.FileName = @WordExe[0]; System.Diagnostics.Process WordProcess = new System.Diagnostics.Process(); WordProcess = System.Diagnostics.Process.Start(psi); WordProcess.EnableRaisingEvents = true; WordProcess.WaitForExit(10000); System.Diagnostics.Process.Start(txt_websiteAddress.Text); Please Help! Thanks!

                M Offline
                M Offline
                mav northwind
                wrote on last edited by
                #27

                Hi! I just tried this and it works as expected using this code:

                // You can simply use the name of the document to open...
                ProcessStartInfo psi = new ProcessStartInfo("C:\\Document.doc");
                MessageBox.Show("Starting...");
                Process p = Process.Start(psi);
                p.WaitForExit();
                MessageBox.Show("Finished");

                The second message box appears as soon as I close Word. Does this help?

                Regards, mav -- Black holes are the places where God divided by 0...

                1 Reply Last reply
                0
                • D danielhasdibs

                  I've asked this before, but I didn't get an answer that fixed the problem. Here is what I want to do: I have a program that opens two applications (a Word document, and a hyperlink). It opens one after the other. What I'm trying to do is open the first application (the Word document) and AFTER the user closes the application, open the second one (the hyperlink). I have tried using the boolean HasExited as well as the method WaitForExit(). Neither are working. As soon as the first application opens, the HasExited is false and WaitForExit() does nothing, even when I use the overloaded version. Here is my code: System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; psi.UseShellExecute = false; psi.Arguments = @txt_Wordfile.Text; psi.FileName = @WordExe[0]; System.Diagnostics.Process WordProcess = new System.Diagnostics.Process(); WordProcess = System.Diagnostics.Process.Start(psi); WordProcess.EnableRaisingEvents = true; WordProcess.WaitForExit(10000); System.Diagnostics.Process.Start(txt_websiteAddress.Text); Please Help! Thanks!

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #28

                  Add /n as a parameter to WinWord. http://support.microsoft.com/kb/210565[^]

                  1 Reply Last reply
                  0
                  • D danielhasdibs

                    I've asked this before, but I didn't get an answer that fixed the problem. Here is what I want to do: I have a program that opens two applications (a Word document, and a hyperlink). It opens one after the other. What I'm trying to do is open the first application (the Word document) and AFTER the user closes the application, open the second one (the hyperlink). I have tried using the boolean HasExited as well as the method WaitForExit(). Neither are working. As soon as the first application opens, the HasExited is false and WaitForExit() does nothing, even when I use the overloaded version. Here is my code: System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; psi.UseShellExecute = false; psi.Arguments = @txt_Wordfile.Text; psi.FileName = @WordExe[0]; System.Diagnostics.Process WordProcess = new System.Diagnostics.Process(); WordProcess = System.Diagnostics.Process.Start(psi); WordProcess.EnableRaisingEvents = true; WordProcess.WaitForExit(10000); System.Diagnostics.Process.Start(txt_websiteAddress.Text); Please Help! Thanks!

                    D Offline
                    D Offline
                    danielhasdibs
                    wrote on last edited by
                    #29

                    Ok, I don't want to be one of those jerks who finds the answer and doesn't share so I have the working code here. I don't know why this didn't work before, but it didn't, and now it does, so I'm happy. System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; psi.UseShellExecute = false; psi.Arguments = txt_Wordfile.Text + " /n"; psi.FileName = WordExe; //string System.Diagnostics.Process WordProcess = new System.Diagnostics.Process(); WordProcess = System.Diagnostics.Process.Start(psi); WordProcess.EnableRaisingEvents = true; while (!WordProcess.HasExited) { ; } WordProcess.Kill(); System.Diagnostics.Process.Start(txt_website.Text); Thanks everyone for your help!

                    M 1 Reply Last reply
                    0
                    • D danielhasdibs

                      Ok, I don't want to be one of those jerks who finds the answer and doesn't share so I have the working code here. I don't know why this didn't work before, but it didn't, and now it does, so I'm happy. System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; psi.UseShellExecute = false; psi.Arguments = txt_Wordfile.Text + " /n"; psi.FileName = WordExe; //string System.Diagnostics.Process WordProcess = new System.Diagnostics.Process(); WordProcess = System.Diagnostics.Process.Start(psi); WordProcess.EnableRaisingEvents = true; while (!WordProcess.HasExited) { ; } WordProcess.Kill(); System.Diagnostics.Process.Start(txt_website.Text); Thanks everyone for your help!

                      M Offline
                      M Offline
                      Martin 0
                      wrote on last edited by
                      #30

                      Hello, Just an addintional info. As the Process class implements IDisposable you should call Dispose() to free the memory. You could use an using-block for that reason. using(System.Diagnostics.Process WordProcess = new System.Diagnostics.Process()) { WordProcess = System.Diagnostics.Process.Start(psi); ... } I don't think there is a need to call the "Kill" method after "HasExited" is "true". Same with "EnableRaisingEvents", only needed if you want to handle the "Exited" event. P.S.: Strange, that WaitForExit is not working here.

                      All the best, Martin

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups