Cant put a wait on external application?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • FLX
    New Member
    • May 2007
    • 11

    Cant put a wait on external application?

    Hello all,

    I have problems letting this application wait. I tried everything in my knowledge.
    Here is the code:

    Code:
                        System.Diagnostics.Process procdemux = new System.Diagnostics.Process();
                        procdemux.StartInfo.UseShellExecute = true;
                        procdemux.StartInfo.CreateNoWindow = false;
                        procdemux.StartInfo.RedirectStandardOutput = false;
                        procdemux.StartInfo.FileName = currentPath + "\\res\\demux.bat";
                        procdemux.Start();
                        procdemux.WaitForExit(3000);
                        procdemux.Close();
    this code follows it up:

    Code:
                System.Diagnostics.Process procencode = new System.Diagnostics.Process();
                procencode.StartInfo.UseShellExecute = true;
                procencode.StartInfo.CreateNoWindow = false;
                procencode.StartInfo.RedirectStandardOutput = false;
                procencode.StartInfo.FileName = currentPath + "\\res\\encode.bat";
                procencode.Start();
                procencode.WaitForExit(3000);
                procencode.Close();
    Can someone please help me?

    Thanks in advance!

    Regards,

    Dennis
  • FLX
    New Member
    • May 2007
    • 11

    #2
    please, someone?

    Regards,

    Dennis

    Comment

    • Atran
      Contributor
      • May 2007
      • 319

      #3
      Hello, I do not know this way, but I learn another way to stop your app:

      Code:
      //Stop your app for 1000 milliseconds.
      System.Threading.Thread.Sleep(1000);
      This code will stop your app for 1 sec.
      --------------------------------------------------------
      But If you mean to wait for load something, I do not know.
      "TRScheel" user help me with the code I give you.
      Hope this help you.

      Comment

      • mwalts
        New Member
        • May 2007
        • 38

        #4
        Originally posted by Atran
        Hello, I do not know this way, but I learn another way to stop your app:

        Code:
        //Stop your app for 1000 milliseconds.
        System.Threading.Thread.Sleep(1000);
        This code will stop your app for 1 sec.
        --------------------------------------------------------
        But If you mean to wait for load something, I do not know.
        "TRScheel" user help me with the code I give you.
        Hope this help you.
        If you don't supply a time for the WaitForExit() call it will wait for as long as it takes the application to exit. Could you try explaining your problem a little bit better? I'm not sure I get what your asking.

        -mwalts

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          You're currently only waiting 3 seconds before you are manually closing that process. Seems a bit quick?

          Comment

          • rancid11
            New Member
            • Jun 2007
            • 3

            #6
            I have a similar problem. I want to run an external app for 20 seconds and then to close.

            i am using waitforexit(200 00) but it isn't working.
            can u tell me how to close an application after 20 seconds ???
            pls help me


            private void button2_Click(o bject sender, EventArgs e)
            {

            System.Diagnost ics.Process p = new System.Diagnost ics.Process();
            p.StartInfo.Fil eName = @"calc.exe";
            p.Start();
            p.WaitForExit(2 0000);


            }

            Comment

            • blackjack2150
              New Member
              • Feb 2007
              • 79

              #7
              Originally posted by rancid11
              I have a similar problem. I want to run an external app for 20 seconds and then to close.

              i am using waitforexit(200 00) but it isn't working.
              can u tell me how to close an application after 20 seconds ???
              pls help me


              private void button2_Click(o bject sender, EventArgs e)
              {

              System.Diagnost ics.Process p = new System.Diagnost ics.Process();
              p.StartInfo.Fil eName = @"calc.exe";
              p.Start();
              p.WaitForExit(2 0000);


              }
              This doesn't seem a good approach to me. In Win32 programming we had the WaitForSingleOb ject method, but in .NET....I don't know...yet.

              If the application you're starting is also written by you, then you could create a small protocol using a semaphore file:
              - create a semaphore file
              - call the second app
              - in the second app, before closing delete the semaphore file
              - meanwhile in the main .NET app, check for the existence of the semaphore file every 500 ms or so:

              while(File.Exis ts("sem.txt"))
              {
              Thread.Sleep(50 0);
              }

              Comment

              • rancid11
                New Member
                • Jun 2007
                • 3

                #8
                Originally posted by blackjack2150
                This doesn't seem a good approach to me. In Win32 programming we had the WaitForSingleOb ject method, but in .NET....I don't know...yet.

                If the application you're starting is also written by you, then you could create a small protocol using a semaphore file:
                - create a semaphore file
                - call the second app
                - in the second app, before closing delete the semaphore file
                - meanwhile in the main .NET app, check for the existence of the semaphore file every 500 ms or so:

                while(File.Exis ts("sem.txt"))
                {
                Thread.Sleep(50 0);
                }


                The application I want to terminate after the 20 seconds is SerialMagic, so I don't have access to the code.

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  Try this program flow:
                  Start your external application (using Process)
                  Tell the current application to wait (Thread.Current Thread.Sleep()? ) the desired time
                  Kill the external program (using the Process object from earlier)

                  I am not sure what your problem with the initial code was.
                  An external program was started
                  It was given 3seconds of time to open and run before you closed it

                  Comment

                  • TRScheel
                    Recognized Expert Contributor
                    • Apr 2007
                    • 638

                    #10
                    Why havent I seen this thread earlier?

                    Anyways, this will be useful to you:

                    [code=cpp]
                    private static bool CheckProcess(Pr ocess process)
                    {
                    bool processFinished = true;
                    try
                    {
                    process.Refresh ();
                    foreach (ProcessThread thread in process.Threads )
                    {
                    if (thread.ThreadS tate != System.Diagnost ics.ThreadState .Terminated)
                    processFinished = false;
                    }
                    }
                    catch
                    {
                    processFinished = true;
                    }

                    return processFinished ;
                    }[/code]

                    Just put that in a while loop, so something like:

                    [code=cpp]
                    while (!CheckProcess( process)) { }
                    [/code]


                    If it refreshes the process when its disposed of, it will throw an error, so thats why we have the catch bit just return true. Otherwise, we look to find it when its terminated (fat chance catching right on THAT moment, hence the try/catch).

                    You are much more likely to run process.Refresh () and have it throw an exception than get the ThreadState to == Terminated when you check. Hence, if you want to be REALLY lazy, you could just see if you could refresh the process and see if that was possible. You might want to throw in the Wait and Unknown depending if the program just sits there after its done (and play around with the output to find out when its finished). In the above example, i was running a number of scripts that exited when done, so I knew that if I couldnt talk to it, they were done.
                    Last edited by TRScheel; Jun 26 '07, 06:59 PM. Reason: Described code

                    Comment

                    • TRScheel
                      Recognized Expert Contributor
                      • Apr 2007
                      • 638

                      #11
                      You can put a thread.sleep(#) in the loop to lessen the CPU load of sitting there just looping. Something like 100, or 250, shouldnt be too noticable.

                      Comment

                      • blackjack2150
                        New Member
                        • Feb 2007
                        • 79

                        #12
                        Here's another idea:

                        Do a while loop in which you check to see that the process you started is still running. If not, break out of the loop and continue on else loop again.
                        This way you don't have to guess how long it will take.

                        The Process class has all you need for this.

                        Comment

                        • TRScheel
                          Recognized Expert Contributor
                          • Apr 2007
                          • 638

                          #13
                          Originally posted by blackjack2150
                          Here's another idea:

                          Do a while loop in which you check to see that the process you started is still running. If not, break out of the loop and continue on else loop again.
                          This way you don't have to guess how long it will take.

                          The Process class has all you need for this.
                          I fooled around with the process.HasExit ed but the problem exists that the program might just sit there doing nothing. Its threadstate gets stuck at unknown, wait, or something else. What's it doing? Waiting for user input? Printing? Done? Its a tough issue to take up depending on the program you are using.

                          Comment

                          Working...