Waiting for process end , how?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob

    Waiting for process end , how?

    Process.start(" Mydoc.doc") starts Word with the file. I need to wait for
    Word to be closed before more code can execute in my app. How can I do this?

    Thanks for any help
    Bob


  • Cor Ligthert [MVP]

    #2
    Re: Waiting for process end , how?

    Bob,

    How about to use the wait for exit in the process class to wait on the exit
    of that process.

    :-)

    Sets the period of time to wait for the associated process to exit, and blocks the current thread of execution until the time has elapsed or the process has exited. To avoid blocking the current thread, use the Exited event. For code examples, see the StandardError and the ExitCode property reference pages.


    I hope this helps,

    Cor


    "Bob" <bdufour@sgiims .comschreef in bericht
    news:uSv4E59vGH A.3392@TK2MSFTN GP04.phx.gbl...
    Process.start(" Mydoc.doc") starts Word with the file. I need to wait for
    Word to be closed before more code can execute in my app. How can I do
    this?
    >
    Thanks for any help
    Bob
    >

    Comment

    • Bob

      #3
      Re: Waiting for process end , how?

      I been trying to do that with the following code
      Dim pr As Process

      pr.Start(Fname) where Fname is the filename to use with the process.

      However I get a message in the IDE saying that Access of Shared member,
      constant member.... etc. qualifying expression will not be evaluated.

      Got any code snippets to do this? It seems straightforward but <GGGG:-)

      Thanks for your help.

      Bob



      "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
      news:ev8wQ89vGH A.4932@TK2MSFTN GP06.phx.gbl...
      Bob,
      >
      How about to use the wait for exit in the process class to wait on the
      exit of that process.
      >
      :-)
      >
      Sets the period of time to wait for the associated process to exit, and blocks the current thread of execution until the time has elapsed or the process has exited. To avoid blocking the current thread, use the Exited event. For code examples, see the StandardError and the ExitCode property reference pages.

      >
      I hope this helps,
      >
      Cor
      >
      >
      "Bob" <bdufour@sgiims .comschreef in bericht
      news:uSv4E59vGH A.3392@TK2MSFTN GP04.phx.gbl...
      >Process.start( "Mydoc.doc" ) starts Word with the file. I need to wait for
      >Word to be closed before more code can execute in my app. How can I do
      >this?
      >>
      >Thanks for any help
      >Bob
      >>
      >
      >

      Comment

      • Tom Shelton

        #4
        Re: Waiting for process end , how?


        Bob wrote:
        I been trying to do that with the following code
        Dim pr As Process
        >
        pr.Start(Fname) where Fname is the filename to use with the process.
        >
        However I get a message in the IDE saying that Access of Shared member,
        constant member.... etc. qualifying expression will not be evaluated.
        >
        Got any code snippets to do this? It seems straightforward but <GGGG:-)
        >
        Thanks for your help.
        >
        Bob
        Bob - here is a simple console application that demonstrates this
        method:

        Option Explicit On
        Option Strict On

        Imports System
        Imports System.Diagnost ics

        Module Module1

        Public Sub Main()
        Dim p As Process = Process.Start(" notepad.exe")
        p.WaitForExit()
        Console.WriteLi ne("Done")
        End Sub

        End Module


        This is a blocking operation... Another way to do this, whithout
        blocking would be:

        Option Explicit On
        Option Strict On

        Imports System
        Imports System.Threadin g
        Imports System.Diagnost ics

        Module Module1
        Private WithEvents p As New Process
        Private exited As Boolean = False

        Public Sub Main()
        p.EnableRaising Events = True
        p.StartInfo.Fil eName = "notepad.ex e"
        p.Start()

        While Not exited
        Thread.Sleep(10 00)
        Console.WriteLi ne("Running!")
        End While

        Console.WriteLi ne("Done")
        End Sub

        Private Sub NotepadExited(B yVal sender As Object, ByVal e As
        EventArgs) Handles p.Exited
        exited = True
        End Sub

        End Module

        Anwyay - HTH,

        --
        Tom Shelton [MVP]

        Comment

        • Cor Ligthert [MVP]

          #5
          Re: Waiting for process end , how?

          Tom,

          Exciting

          :-)

          Cor

          "Tom Shelton" <tom@mtogden.co mschreef in bericht
          news:1155659311 .605212.36440@7 4g2000cwt.googl egroups.com...
          >
          Bob wrote:
          >I been trying to do that with the following code
          >Dim pr As Process
          >>
          >pr.Start(Fname ) where Fname is the filename to use with the process.
          >>
          >However I get a message in the IDE saying that Access of Shared member,
          >constant member.... etc. qualifying expression will not be evaluated.
          >>
          >Got any code snippets to do this? It seems straightforward but <GGGG:-)
          >>
          >Thanks for your help.
          >>
          >Bob
          >
          Bob - here is a simple console application that demonstrates this
          method:
          >
          Option Explicit On
          Option Strict On
          >
          Imports System
          Imports System.Diagnost ics
          >
          Module Module1
          >
          Public Sub Main()
          Dim p As Process = Process.Start(" notepad.exe")
          p.WaitForExit()
          Console.WriteLi ne("Done")
          End Sub
          >
          End Module
          >
          >
          This is a blocking operation... Another way to do this, whithout
          blocking would be:
          >
          Option Explicit On
          Option Strict On
          >
          Imports System
          Imports System.Threadin g
          Imports System.Diagnost ics
          >
          Module Module1
          Private WithEvents p As New Process
          Private exited As Boolean = False
          >
          Public Sub Main()
          p.EnableRaising Events = True
          p.StartInfo.Fil eName = "notepad.ex e"
          p.Start()
          >
          While Not exited
          Thread.Sleep(10 00)
          Console.WriteLi ne("Running!")
          End While
          >
          Console.WriteLi ne("Done")
          End Sub
          >
          Private Sub NotepadExited(B yVal sender As Object, ByVal e As
          EventArgs) Handles p.Exited
          exited = True
          End Sub
          >
          End Module
          >
          Anwyay - HTH,
          >
          --
          Tom Shelton [MVP]
          >

          Comment

          • Bob

            #6
            Re: Waiting for process end , how?

            Thanks Tom Gonna give it a try.
            Bob
            "Tom Shelton" <tom@mtogden.co mwrote in message
            news:1155659311 .605212.36440@7 4g2000cwt.googl egroups.com...
            >
            Bob wrote:
            >I been trying to do that with the following code
            >Dim pr As Process
            >>
            >pr.Start(Fname ) where Fname is the filename to use with the process.
            >>
            >However I get a message in the IDE saying that Access of Shared member,
            >constant member.... etc. qualifying expression will not be evaluated.
            >>
            >Got any code snippets to do this? It seems straightforward but <GGGG:-)
            >>
            >Thanks for your help.
            >>
            >Bob
            >
            Bob - here is a simple console application that demonstrates this
            method:
            >
            Option Explicit On
            Option Strict On
            >
            Imports System
            Imports System.Diagnost ics
            >
            Module Module1
            >
            Public Sub Main()
            Dim p As Process = Process.Start(" notepad.exe")
            p.WaitForExit()
            Console.WriteLi ne("Done")
            End Sub
            >
            End Module
            >
            >
            This is a blocking operation... Another way to do this, whithout
            blocking would be:
            >
            Option Explicit On
            Option Strict On
            >
            Imports System
            Imports System.Threadin g
            Imports System.Diagnost ics
            >
            Module Module1
            Private WithEvents p As New Process
            Private exited As Boolean = False
            >
            Public Sub Main()
            p.EnableRaising Events = True
            p.StartInfo.Fil eName = "notepad.ex e"
            p.Start()
            >
            While Not exited
            Thread.Sleep(10 00)
            Console.WriteLi ne("Running!")
            End While
            >
            Console.WriteLi ne("Done")
            End Sub
            >
            Private Sub NotepadExited(B yVal sender As Object, ByVal e As
            EventArgs) Handles p.Exited
            exited = True
            End Sub
            >
            End Module
            >
            Anwyay - HTH,
            >
            --
            Tom Shelton [MVP]
            >

            Comment

            • Bob

              #7
              Re: Waiting for process end , how?

              Thanks Tom,
              Excellent info for me.
              I've been testing the first snippet of code using Word instead of Notepad
              Dim pr As Process = Process.Start(" Mydoc.doc") 'Starts Word and opens the
              file OK
              pr.WaitForExit( )
              Console.WriteLi ne("Done")
              However when execution hits the line p.WaitForExit() I get an unhandled
              exception Object not set to an instance of an object. Normally this means
              that I did not instantiate the pr object and indeed the code does not do
              that (it would need the New keyword in he declaration) however with this
              shared class you can not use the new keyword in the declaration.

              How could I end up gettng Word to open up and have to wait before going back
              to my application form that the process is closed. I look at the docs and I
              see that Waitforexit does interrupt the calling thread and this is indeed
              whats needed. I just don't see how to code it.

              Any help is really, really appreciated.

              Bob









              "Tom Shelton" <tom@mtogden.co mwrote in message
              news:1155659311 .605212.36440@7 4g2000cwt.googl egroups.com...
              >
              Bob wrote:
              >I been trying to do that with the following code
              >Dim pr As Process
              >>
              >pr.Start(Fname ) where Fname is the filename to use with the process.
              >>
              >However I get a message in the IDE saying that Access of Shared member,
              >constant member.... etc. qualifying expression will not be evaluated.
              >>
              >Got any code snippets to do this? It seems straightforward but <GGGG:-)
              >>
              >Thanks for your help.
              >>
              >Bob
              >
              Bob - here is a simple console application that demonstrates this
              method:
              >
              Option Explicit On
              Option Strict On
              >
              Imports System
              Imports System.Diagnost ics
              >
              Module Module1
              >
              Public Sub Main()
              Dim p As Process = Process.Start(" notepad.exe")
              p.WaitForExit()
              Console.WriteLi ne("Done")
              End Sub
              >
              End Module
              >
              >
              This is a blocking operation... Another way to do this, whithout
              blocking would be:
              >
              Option Explicit On
              Option Strict On
              >
              Imports System
              Imports System.Threadin g
              Imports System.Diagnost ics
              >
              Module Module1
              Private WithEvents p As New Process
              Private exited As Boolean = False
              >
              Public Sub Main()
              p.EnableRaising Events = True
              p.StartInfo.Fil eName = "notepad.ex e"
              p.Start()
              >
              While Not exited
              Thread.Sleep(10 00)
              Console.WriteLi ne("Running!")
              End While
              >
              Console.WriteLi ne("Done")
              End Sub
              >
              Private Sub NotepadExited(B yVal sender As Object, ByVal e As
              EventArgs) Handles p.Exited
              exited = True
              End Sub
              >
              End Module
              >
              Anwyay - HTH,
              >
              --
              Tom Shelton [MVP]
              >

              Comment

              • Tom Shelton

                #8
                Re: Waiting for process end , how?

                Bob wrote:
                Thanks Tom,
                Excellent info for me.
                I've been testing the first snippet of code using Word instead of Notepad
                Dim pr As Process = Process.Start(" Mydoc.doc") 'Starts Word and opens the
                file OK
                pr.WaitForExit( )
                Console.WriteLi ne("Done")
                However when execution hits the line p.WaitForExit() I get an unhandled
                exception Object not set to an instance of an object. Normally this means
                that I did not instantiate the pr object and indeed the code does not do
                that (it would need the New keyword in he declaration) however with this
                shared class you can not use the new keyword in the declaration.
                >
                How could I end up gettng Word to open up and have to wait before going back
                to my application form that the process is closed. I look at the docs and I
                see that Waitforexit does interrupt the calling thread and this is indeed
                whats needed. I just don't see how to code it.
                >
                Any help is really, really appreciated.
                >
                Bob

                Bob - the example I wrote works fine for me... With word as well. I
                just tested it here with word to be certain.

                The call to process.start returns an instance of the process class, so
                there is no shared methods involved. You do not call this on the
                Process class, but on the instance returned by the Process.Start
                method.

                I think what we need is the shortest snippet of ACTUAL code that
                demonstrates the issue, since I can not replicate this problem.

                --
                Tom Shelton [MVP]

                Comment

                Working...