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!

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

    Maybe this WordProcess.EnableRaisingEvents = true; is the problem, comment it out and try it again. Nope, it's not that either. :confused:

    modified on Friday, February 8, 2008 3:59 PM

    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
      #15

      Dang. It works as expected with Notepad, but not with Word -- Word must act differently.

      D 2 Replies Last reply
      0
      • P PIEBALDconsult

        Dang. It works as expected with Notepad, but not with Word -- Word must act differently.

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

        Bummer! If you or anyone else knows how I can keep track of (tell when it's closed by the user) a Word document, I would be greatly appreciative!

        P 1 Reply Last reply
        0
        • D DaveyM69

          HasExited... = Has the user exited? The user HASN'T exited (false) The user HAS existed (true) Therefore, when HasExited==true then the application has exited!

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

          For some reason, this does not work with MS Word. (at least on my computer)

          1 Reply Last reply
          0
          • P PIEBALDconsult

            Dang. It works as expected with Notepad, but not with Word -- Word must act differently.

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

            If you can get it to work the other way around (hyperlink opens first, waits for the user to exit, then opens the Word document) that would work for me, too. thanks.

            1 Reply Last reply
            0
            • D danielhasdibs

              Bummer! If you or anyone else knows how I can keep track of (tell when it's closed by the user) a Word document, I would be greatly appreciative!

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

              More info: Outlook uses Word as its editor so when Outlook is open, Word is running, so then the Process.Start merely passes the file to the running instance of Word and exits. Close Outlook, and try again. It worked for me that way, but I still don't have a decent workaround.

              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
                DaveyM69
                wrote on last edited by
                #20

                After

                danielhasdibs wrote:

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

                add

                WordProcess.StandardOutput.ReadToEnd();

                Works on my system (no need for the WaitForExit method call). For some reason the Exited event isn't raised but the HasExited property is correctly set. This is working...

                System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
                psi.RedirectStandardOutput = true;
                psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
                psi.UseShellExecute = false;
                psi.Arguments = "Help.txt";
                psi.FileName = "C:\\Program Files\\Microsoft Office\\Office12\\WINWORD.EXE";

                        System.Diagnostics.Process WordProcess = new System.Diagnostics.Process();
                        WordProcess.EnableRaisingEvents = true;
                        WordProcess = System.Diagnostics.Process.Start(psi);
                        WordProcess.StandardOutput.ReadToEnd();
                        Console.WriteLine(WordProcess.HasExited);
                        System.Diagnostics.Process.Start("Explorer.exe", "http://google.com");
                

                Dave

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

                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!

                  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