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. Process Exit problem !

Process Exit problem !

Scheduled Pinned Locked Moved C#
helpcsharpc++visual-studiotutorial
18 Posts 4 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.
  • N Offline
    N Offline
    Nematjon Rahmanov
    wrote on last edited by
    #1

    Hi. I have problem with process exit. I'm using this code :

    Process proc = new Process();
    proc.StartInfo.FileName = str;
    proc.Start();
    proc.WaitForExit();
    DoSomeThings();

    This code sometimes not working. For example ,I have opening Visual Studio. I want run process *.cs or *.cpp (Visual studio files) files. That files opened last Visual Studio.After that i don't know my process running or exited. And i get InvalidOperationException. How i can know my process running or exited? Please help me. Thanks, Nematjon.

    We are haven't bug,just temporarily undecided problems.

    L realJSOPR 2 Replies Last reply
    0
    • N Nematjon Rahmanov

      Hi. I have problem with process exit. I'm using this code :

      Process proc = new Process();
      proc.StartInfo.FileName = str;
      proc.Start();
      proc.WaitForExit();
      DoSomeThings();

      This code sometimes not working. For example ,I have opening Visual Studio. I want run process *.cs or *.cpp (Visual studio files) files. That files opened last Visual Studio.After that i don't know my process running or exited. And i get InvalidOperationException. How i can know my process running or exited? Please help me. Thanks, Nematjon.

      We are haven't bug,just temporarily undecided problems.

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

      from MSDN: "Process.Start() returns true if a process resource is started; false if no new process resource is started (for example, if an existing process is reused)." :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
      All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


      N 1 Reply Last reply
      0
      • L Luc Pattyn

        from MSDN: "Process.Start() returns true if a process resource is started; false if no new process resource is started (for example, if an existing process is reused)." :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
        All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


        N Offline
        N Offline
        Nematjon Rahmanov
        wrote on last edited by
        #3

        Hi Luc. Thanks for you reply. I think you don't understand me(soory , my english skils not so good ). My process started, but i want to know when process exited? Thanks.

        We are haven't bug,just temporarily undecided problems.

        L 1 Reply Last reply
        0
        • N Nematjon Rahmanov

          Hi Luc. Thanks for you reply. I think you don't understand me(soory , my english skils not so good ). My process started, but i want to know when process exited? Thanks.

          We are haven't bug,just temporarily undecided problems.

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

          your code should work fine if the process wasn't already running. AFAIK it will fail if the process was already running (Start returns false then, and every other Process method will fail). Solution: if Start returns false, you must get the process from GetProcessesByName() and then WaitForExit() on the correct one. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
          All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


          N 1 Reply Last reply
          0
          • N Nematjon Rahmanov

            Hi. I have problem with process exit. I'm using this code :

            Process proc = new Process();
            proc.StartInfo.FileName = str;
            proc.Start();
            proc.WaitForExit();
            DoSomeThings();

            This code sometimes not working. For example ,I have opening Visual Studio. I want run process *.cs or *.cpp (Visual studio files) files. That files opened last Visual Studio.After that i don't know my process running or exited. And i get InvalidOperationException. How i can know my process running or exited? Please help me. Thanks, Nematjon.

            We are haven't bug,just temporarily undecided problems.

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            You could do this:

            public void StartProcess()
            {
            Process proc = new Process....
            // do other appropriate initialization

            proc.EnableRaisingEvents = true;
            proc.Exited += new EventHandler(process\_Exited);
            proc.Start();
            

            }

            private void process_Exited(object sender, EventArgs e)
            {
            Process process = sender as Process;
            // you can check the Process.ExitCode if appropriate
            }

            .45 ACP - because shooting twice is just silly
            -----
            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

            N 1 Reply Last reply
            0
            • L Luc Pattyn

              your code should work fine if the process wasn't already running. AFAIK it will fail if the process was already running (Start returns false then, and every other Process method will fail). Solution: if Start returns false, you must get the process from GetProcessesByName() and then WaitForExit() on the correct one. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
              All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


              N Offline
              N Offline
              Nematjon Rahmanov
              wrote on last edited by
              #6

              Thanks for you reply Luc. Yes , you are right. Start() fuction returned false.

              Process proc = new Process();
              proc.StartInfo.FileName ="c:\\test.cs";
              proc.Start(); // False
              proc.WaitForExit(); // InvalidOperationException

              My file is -test.cs. It default opened with Visual Studio (devenv.exe). In Visual Studio can be more files (tab page . for example , test.cs, Program.cs, Main.cs ,...). How i can detect test.cs file closed? And my file should be anything and they can open anything app. I think not possible GetProcessesByName(). Thanks.

              We are haven't bug,just temporarily undecided problems.

              L realJSOPR 2 Replies Last reply
              0
              • N Nematjon Rahmanov

                Thanks for you reply Luc. Yes , you are right. Start() fuction returned false.

                Process proc = new Process();
                proc.StartInfo.FileName ="c:\\test.cs";
                proc.Start(); // False
                proc.WaitForExit(); // InvalidOperationException

                My file is -test.cs. It default opened with Visual Studio (devenv.exe). In Visual Studio can be more files (tab page . for example , test.cs, Program.cs, Main.cs ,...). How i can detect test.cs file closed? And my file should be anything and they can open anything app. I think not possible GetProcessesByName(). Thanks.

                We are haven't bug,just temporarily undecided problems.

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

                it will not always work; e.g. Visual Studio files get launched through devenv.exe, however this isn't the process that you will use in the end, it is just a go-between, and it will close long before your VS closes. Have a look with Task Manager! :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


                1 Reply Last reply
                0
                • realJSOPR realJSOP

                  You could do this:

                  public void StartProcess()
                  {
                  Process proc = new Process....
                  // do other appropriate initialization

                  proc.EnableRaisingEvents = true;
                  proc.Exited += new EventHandler(process\_Exited);
                  proc.Start();
                  

                  }

                  private void process_Exited(object sender, EventArgs e)
                  {
                  Process process = sender as Process;
                  // you can check the Process.ExitCode if appropriate
                  }

                  .45 ACP - because shooting twice is just silly
                  -----
                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                  N Offline
                  N Offline
                  Nematjon Rahmanov
                  wrote on last edited by
                  #8

                  Hi. Thanks for reply. I was try this , but same results. I not yet know when my file closed ?

                  We are haven't bug,just temporarily undecided problems.

                  realJSOPR D 2 Replies Last reply
                  0
                  • N Nematjon Rahmanov

                    Hi. Thanks for reply. I was try this , but same results. I not yet know when my file closed ?

                    We are haven't bug,just temporarily undecided problems.

                    realJSOPR Offline
                    realJSOPR Offline
                    realJSOP
                    wrote on last edited by
                    #9

                    Well, without seeing all of the related code, we can't help you.

                    .45 ACP - because shooting twice is just silly
                    -----
                    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                    -----
                    "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                    N 1 Reply Last reply
                    0
                    • realJSOPR realJSOP

                      Well, without seeing all of the related code, we can't help you.

                      .45 ACP - because shooting twice is just silly
                      -----
                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                      -----
                      "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                      N Offline
                      N Offline
                      Nematjon Rahmanov
                      wrote on last edited by
                      #10

                      Thanks for your honest to goodness answer.:rose:

                      We are haven't bug,just temporarily undecided problems.

                      1 Reply Last reply
                      0
                      • N Nematjon Rahmanov

                        Thanks for you reply Luc. Yes , you are right. Start() fuction returned false.

                        Process proc = new Process();
                        proc.StartInfo.FileName ="c:\\test.cs";
                        proc.Start(); // False
                        proc.WaitForExit(); // InvalidOperationException

                        My file is -test.cs. It default opened with Visual Studio (devenv.exe). In Visual Studio can be more files (tab page . for example , test.cs, Program.cs, Main.cs ,...). How i can detect test.cs file closed? And my file should be anything and they can open anything app. I think not possible GetProcessesByName(). Thanks.

                        We are haven't bug,just temporarily undecided problems.

                        realJSOPR Offline
                        realJSOPR Offline
                        realJSOP
                        wrote on last edited by
                        #11

                        I think I see your problem. You're mixing ShellExecute with running a process. When you run a process, the StartInfo.FileName should be the name of the exe file you want to execute. In your particular case, you want to run devenv.exe with C:\test.cs as a commandline parameter. I think if you do that, your code will do what you expect it to do. BTW, when you specify commandline parameters, make sure you put double-quotes in the string if your commandline contains spaces, like so:

                        "\"c:\test.cs\""

                        .45 ACP - because shooting twice is just silly
                        -----
                        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                        -----
                        "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                        N 1 Reply Last reply
                        0
                        • realJSOPR realJSOP

                          I think I see your problem. You're mixing ShellExecute with running a process. When you run a process, the StartInfo.FileName should be the name of the exe file you want to execute. In your particular case, you want to run devenv.exe with C:\test.cs as a commandline parameter. I think if you do that, your code will do what you expect it to do. BTW, when you specify commandline parameters, make sure you put double-quotes in the string if your commandline contains spaces, like so:

                          "\"c:\test.cs\""

                          .45 ACP - because shooting twice is just silly
                          -----
                          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                          -----
                          "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                          N Offline
                          N Offline
                          Nematjon Rahmanov
                          wrote on last edited by
                          #12

                          Thanks. I try

                          "\"c:\test.cs\""

                          . I just want know when test.cs file closed. Thanks.

                          We are haven't bug,just temporarily undecided problems.

                          realJSOPR 1 Reply Last reply
                          0
                          • N Nematjon Rahmanov

                            Thanks. I try

                            "\"c:\test.cs\""

                            . I just want know when test.cs file closed. Thanks.

                            We are haven't bug,just temporarily undecided problems.

                            realJSOPR Offline
                            realJSOPR Offline
                            realJSOP
                            wrote on last edited by
                            #13

                            OHHHHHHHHH. I thought you were trying to determine when the process ended. You need to set up a thread that tries to open the file access set to FileShare.None. Once it doesn't throw an exception, the file's been closed. Do something like this:

                            bool closed = false;
                            do
                            {
                            try
                            {
                            // try to open a file stream
                            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
                            // if we get here, we didn't get an exception and the file is ready to be used
                            closed = true;
                            }
                            catch (Exception ex)
                            {
                            if (ex != null) {}
                            }
                            } while (!closed)

                            You could use a BackgroundWorker object since it can post an event when the thread has completed, and you can react to that event.

                            .45 ACP - because shooting twice is just silly
                            -----
                            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                            -----
                            "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                            N 1 Reply Last reply
                            0
                            • realJSOPR realJSOP

                              OHHHHHHHHH. I thought you were trying to determine when the process ended. You need to set up a thread that tries to open the file access set to FileShare.None. Once it doesn't throw an exception, the file's been closed. Do something like this:

                              bool closed = false;
                              do
                              {
                              try
                              {
                              // try to open a file stream
                              using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
                              // if we get here, we didn't get an exception and the file is ready to be used
                              closed = true;
                              }
                              catch (Exception ex)
                              {
                              if (ex != null) {}
                              }
                              } while (!closed)

                              You could use a BackgroundWorker object since it can post an event when the thread has completed, and you can react to that event.

                              .45 ACP - because shooting twice is just silly
                              -----
                              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                              -----
                              "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                              N Offline
                              N Offline
                              Nematjon Rahmanov
                              wrote on last edited by
                              #14

                              Hi John. Thanks for reply. My code :

                              Process proc = new Process();
                              proc.StartInfo.FileName = fileName;//c:\\test.cs
                              proc.Start();

                              bool closed = false;
                              do
                              {
                              try
                              {
                              // try to open a file stream
                              using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
                              // if we get here, we didn't get an exception and the file is ready to be used
                              closed = true;
                              }
                              catch (Exception ex)
                              {
                              if (ex != null) {}
                              }
                              } while (!closed);

                              Everytime i get closed=true. So i can't detect when file closed. My scenario is correct ? Thanks.

                              We are haven't bug,just temporarily undecided problems.

                              realJSOPR 1 Reply Last reply
                              0
                              • N Nematjon Rahmanov

                                Hi John. Thanks for reply. My code :

                                Process proc = new Process();
                                proc.StartInfo.FileName = fileName;//c:\\test.cs
                                proc.Start();

                                bool closed = false;
                                do
                                {
                                try
                                {
                                // try to open a file stream
                                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
                                // if we get here, we didn't get an exception and the file is ready to be used
                                closed = true;
                                }
                                catch (Exception ex)
                                {
                                if (ex != null) {}
                                }
                                } while (!closed);

                                Everytime i get closed=true. So i can't detect when file closed. My scenario is correct ? Thanks.

                                We are haven't bug,just temporarily undecided problems.

                                realJSOPR Offline
                                realJSOPR Offline
                                realJSOP
                                wrote on last edited by
                                #15

                                Ya know, I don't have your code, so you're going to have to do the work here. As a test to see if the do/while loop is working, open the fie in another program (like notepad), and start your do/while loop in a test app (run it under the debugger). I gave that code from memory - you may have to tweak it a little to make it actually do what you want. Did you put the loop into a thread like I suggested?

                                .45 ACP - because shooting twice is just silly
                                -----
                                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                                -----
                                "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                                N 1 Reply Last reply
                                0
                                • realJSOPR realJSOP

                                  Ya know, I don't have your code, so you're going to have to do the work here. As a test to see if the do/while loop is working, open the fie in another program (like notepad), and start your do/while loop in a test app (run it under the debugger). I gave that code from memory - you may have to tweak it a little to make it actually do what you want. Did you put the loop into a thread like I suggested?

                                  .45 ACP - because shooting twice is just silly
                                  -----
                                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                                  -----
                                  "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                                  N Offline
                                  N Offline
                                  Nematjon Rahmanov
                                  wrote on last edited by
                                  #16

                                  Thanks John. Yeah , like notepad , MS Office and so many files working well. But some files like cs , cpp files not working ? Thanks , again for reply.

                                  We are haven't bug,just temporarily undecided problems.

                                  realJSOPR 1 Reply Last reply
                                  0
                                  • N Nematjon Rahmanov

                                    Thanks John. Yeah , like notepad , MS Office and so many files working well. But some files like cs , cpp files not working ? Thanks , again for reply.

                                    We are haven't bug,just temporarily undecided problems.

                                    realJSOPR Offline
                                    realJSOPR Offline
                                    realJSOP
                                    wrote on last edited by
                                    #17

                                    Maybe the program that's associated with .CS files is loading the file, and then closing it right away. So, if that's the case, that would make the file itself available, but you wouldn't know if it had been changed. Maybe you need to use a FileSystemWatcher on the .CS file to monitor it for changes until the program you ran in the process object terminates. I'm just grasping at straws here because you haven't fully described the problem.

                                    .45 ACP - because shooting twice is just silly
                                    -----
                                    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                                    -----
                                    "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                                    1 Reply Last reply
                                    0
                                    • N Nematjon Rahmanov

                                      Hi. Thanks for reply. I was try this , but same results. I not yet know when my file closed ?

                                      We are haven't bug,just temporarily undecided problems.

                                      D Offline
                                      D Offline
                                      Dave Kreskowiak
                                      wrote on last edited by
                                      #18

                                      You are trying to determine when Visual Studio closes the file without Visual Studio exiting? You cannot determine that using the Process class or FileSystemWatcher, or any other external component. The only way to figure that out is to write code that talks to Visual Studio itself and ask what the currently open documents are. No, I don't know how to do that.

                                      A guide to posting questions on CodeProject[^]
                                      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                           2006, 2007, 2008
                                      But no longer in 2009...

                                      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