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 Offline
    D Offline
    danielhasdibs
    wrote on last edited by
    #1

    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 D D L G 13 Replies 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
      #2

      danielhasdibs wrote:

      HasExited is false

      That's as it should be.

      D 1 Reply Last reply
      0
      • P PIEBALDconsult

        danielhasdibs wrote:

        HasExited is false

        That's as it should be.

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

        Ok, would you care to explain further, please? Why is HasExited false when the user has not exited the process?

        P D 2 Replies Last reply
        0
        • D danielhasdibs

          Ok, would you care to explain further, please? Why is HasExited false when the user has not exited the process?

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

          To indicate such. Property Value true if the operating system process referenced by the Process component has terminated; otherwise, false. Remarks A value of true for HasExited indicates that the associated process has terminated

          modified on Friday, February 8, 2008 3:11 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!

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

            Ok, is there anyone out there that actually has a solution to the problem?

            G E 2 Replies Last reply
            0
            • D danielhasdibs

              Ok, is there anyone out there that actually has a solution to the problem?

              G Offline
              G Offline
              Gareth H
              wrote on last edited by
              #6

              danielhasdibs, I don't have a solution, but i have an idea. Start a new thread that is passed the process info that you start first, and then do a while loop. eg:

              do
              {
              somet();
              }
              while (!process.HasExited)

              LaunchUrl();

              Only an idea and someone might point out something i have not thought of but have a go and see what happens. Regards, Gareth.

              D 1 Reply Last reply
              0
              • D danielhasdibs

                Ok, is there anyone out there that actually has a solution to the problem?

                E Offline
                E Offline
                Ennis Ray Lynch Jr
                wrote on last edited by
                #7

                Not with the approach you are using. It really is a pain to wait for thread exits in Windows. I usually give up in frustration and rely on load bearing for loops (yuk!).

                Need a C# Consultant? I'm available.
                Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

                1 Reply Last reply
                0
                • D danielhasdibs

                  Ok, would you care to explain further, please? Why is HasExited false when the user has not exited the process?

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

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

                    danielhasdibs wrote:

                    psi.Arguments = @txt_Wordfile.Text; psi.FileName = @WordExe[0];

                    I'm wondering about these lines; why the at signs? Why the [0]? Does Word start as expected? WaitForExit() ought to do what you want.

                    D 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      danielhasdibs wrote:

                      psi.Arguments = @txt_Wordfile.Text; psi.FileName = @WordExe[0];

                      I'm wondering about these lines; why the at signs? Why the [0]? Does Word start as expected? WaitForExit() ought to do what you want.

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

                      I have tried many, many variations of the code above. I'm practically bald from me pulling my hair out. I used the @ symbol because I saw it used in other code, but even without it, HasExited and WaitForExit do not work as expected. Ignore the string array and just assume it is a string (which it is). That's just how I need it for my particular piece of code. However, the result of the code above are as follows: The Word document opens, then immediately the hyperlink opens. There is no pause or anything.

                      P 1 Reply Last reply
                      0
                      • G Gareth H

                        danielhasdibs, I don't have a solution, but i have an idea. Start a new thread that is passed the process info that you start first, and then do a while loop. eg:

                        do
                        {
                        somet();
                        }
                        while (!process.HasExited)

                        LaunchUrl();

                        Only an idea and someone might point out something i have not thought of but have a go and see what happens. Regards, Gareth.

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

                        I tried it without success. Once again, the two applications started: one right after the other. If you have another idea, I'm all ears. Thanks.

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

                          I don't know if it makes a difference, but I'm using MS Visual C# Express 2005. If someone could post a code sample that works for them, that would be awesome! Thanks. UPDATE By the way, please use MS Word as the first application to open and a hyperlink as the second. That seems to make a difference in the code.

                          modified on Friday, February 8, 2008 4:12 PM

                          1 Reply Last reply
                          0
                          • D danielhasdibs

                            I have tried many, many variations of the code above. I'm practically bald from me pulling my hair out. I used the @ symbol because I saw it used in other code, but even without it, HasExited and WaitForExit do not work as expected. Ignore the string array and just assume it is a string (which it is). That's just how I need it for my particular piece of code. However, the result of the code above are as follows: The Word document opens, then immediately the hyperlink opens. There is no pause or anything.

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

                            Have you stepped through the code with the debugger? You could add a System.Threading.Thread.Sleep ( 1000 ) after the Start to give the process time to start. Otherwise I'm stumped.

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