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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Can we read return from a process without waiting its completion?

Can we read return from a process without waiting its completion?

Scheduled Pinned Locked Moved Visual Basic
helpquestionannouncementworkspace
7 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.
  • A Offline
    A Offline
    ATC
    wrote on last edited by
    #1

    With the help of Pete (earlier post) I am able to read & display return lines then close a process through command line (MS-DOS window) with the following codes: Dim ps1 As New Process ps1.StartInfo.FileName = "cmd.exe" ps1.StartInfo.WindowStyle = ProcessWindowStyle.Normal ps1.StartInfo.CreateNoWindow = False ps1.StartInfo.UseShellExecute = False ps1.StartInfo.RedirectStandardOutput = True ps1.StartInfo.RedirectStandardInput = True ps1.Start() Dim s1 As String Dim sIn As StreamWriter = ps1.StandardInput Dim sOut As StreamReader = ps1.StandardOutput sIn.AutoFlush = True sIn.Write("dir/p" & System.Environment.NewLine) Thread.Sleep(100) s1 = sOut.ReadLine ps1.Kill() ListBox.Items.Clear() '// *** Display every return line *** // While s1 <> "End of Line" ListBox.Items.Add(s1) s1 = sOut.ReadLine End While ListBox.Items.Add(s1) However, if the process does not stop then the ListBox can not update data (I think because the process takes more priority then the ListBox?) 1. Can we modify the above codes so that we can update the ListBox without waiting the process completed? 2. If it is not possible ... is there another way to do it? Thanks for any help! :doh:

    D L 2 Replies Last reply
    0
    • A ATC

      With the help of Pete (earlier post) I am able to read & display return lines then close a process through command line (MS-DOS window) with the following codes: Dim ps1 As New Process ps1.StartInfo.FileName = "cmd.exe" ps1.StartInfo.WindowStyle = ProcessWindowStyle.Normal ps1.StartInfo.CreateNoWindow = False ps1.StartInfo.UseShellExecute = False ps1.StartInfo.RedirectStandardOutput = True ps1.StartInfo.RedirectStandardInput = True ps1.Start() Dim s1 As String Dim sIn As StreamWriter = ps1.StandardInput Dim sOut As StreamReader = ps1.StandardOutput sIn.AutoFlush = True sIn.Write("dir/p" & System.Environment.NewLine) Thread.Sleep(100) s1 = sOut.ReadLine ps1.Kill() ListBox.Items.Clear() '// *** Display every return line *** // While s1 <> "End of Line" ListBox.Items.Add(s1) s1 = sOut.ReadLine End While ListBox.Items.Add(s1) However, if the process does not stop then the ListBox can not update data (I think because the process takes more priority then the ListBox?) 1. Can we modify the above codes so that we can update the ListBox without waiting the process completed? 2. If it is not possible ... is there another way to do it? Thanks for any help! :doh:

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

      ATC wrote:

      Thread.Sleep(100) s1 = sOut.ReadLine ps1.Kill()

      I love how you wait one tenth of a second, read a single line of it's output, and then kill the process, whether it's doing something or not. Then you continue and try and read lines from a process you already killed off. You cannot do that. The process object must stay running in order for you to read from it's output stream.

      ATC wrote:

      However, if the process does not stop then the ListBox can not update data

      This is because the UI thread is running the code to read the output stream from the process. Until the read operation and filling of the ListBox is done and your code goes idle again, you're controls cannot repaint themselves because the thread is busy doing other things.

      ATC wrote:

      I think because the process takes more priority then the ListBox?)

      Priority has nothing to do with it. Threading does.

      ATC wrote:

      1. Can we modify the above codes so that we can update the ListBox without waiting the process completed?

      You cant read hasn't has been output, so no. You have to wait for the process to finish. If by this statement you mean that you want the control to repaint itself and continue to fill the ListBox with data, you either have to move the code that reads the output stream to another thread, or you have to read the output stream asynchronously.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      A S 2 Replies Last reply
      0
      • A ATC

        With the help of Pete (earlier post) I am able to read & display return lines then close a process through command line (MS-DOS window) with the following codes: Dim ps1 As New Process ps1.StartInfo.FileName = "cmd.exe" ps1.StartInfo.WindowStyle = ProcessWindowStyle.Normal ps1.StartInfo.CreateNoWindow = False ps1.StartInfo.UseShellExecute = False ps1.StartInfo.RedirectStandardOutput = True ps1.StartInfo.RedirectStandardInput = True ps1.Start() Dim s1 As String Dim sIn As StreamWriter = ps1.StandardInput Dim sOut As StreamReader = ps1.StandardOutput sIn.AutoFlush = True sIn.Write("dir/p" & System.Environment.NewLine) Thread.Sleep(100) s1 = sOut.ReadLine ps1.Kill() ListBox.Items.Clear() '// *** Display every return line *** // While s1 <> "End of Line" ListBox.Items.Add(s1) s1 = sOut.ReadLine End While ListBox.Items.Add(s1) However, if the process does not stop then the ListBox can not update data (I think because the process takes more priority then the ListBox?) 1. Can we modify the above codes so that we can update the ListBox without waiting the process completed? 2. If it is not possible ... is there another way to do it? Thanks for any help! :doh:

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

        Hi, AFAIK the general solution that will cope with both stdout and stderr, no matter how much data is expected, requires several threads: - one thread (other than the GUI thread) that reads lines (not ReadToEnd) from stdout - one more thread (other than the GUI thread) that reads lines from stderr - the necessary Control.InvokeRequired and Control.Invoke() stuff to let them access your GUI - and maybe yet another thread that waits for the process to terminate (if that is relevant to your app). And even when you program it according to the documentation (study it!) you will find it difficult to get it to work properly on Win95/98/Me (IIRC that's how it used to be on .NET 1.0/1.1 anyway). :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


        A 1 Reply Last reply
        0
        • D Dave Kreskowiak

          ATC wrote:

          Thread.Sleep(100) s1 = sOut.ReadLine ps1.Kill()

          I love how you wait one tenth of a second, read a single line of it's output, and then kill the process, whether it's doing something or not. Then you continue and try and read lines from a process you already killed off. You cannot do that. The process object must stay running in order for you to read from it's output stream.

          ATC wrote:

          However, if the process does not stop then the ListBox can not update data

          This is because the UI thread is running the code to read the output stream from the process. Until the read operation and filling of the ListBox is done and your code goes idle again, you're controls cannot repaint themselves because the thread is busy doing other things.

          ATC wrote:

          I think because the process takes more priority then the ListBox?)

          Priority has nothing to do with it. Threading does.

          ATC wrote:

          1. Can we modify the above codes so that we can update the ListBox without waiting the process completed?

          You cant read hasn't has been output, so no. You have to wait for the process to finish. If by this statement you mean that you want the control to repaint itself and continue to fill the ListBox with data, you either have to move the code that reads the output stream to another thread, or you have to read the output stream asynchronously.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          A Offline
          A Offline
          ATC
          wrote on last edited by
          #4

          Hi Dave Now I undestand more about my problem. I will try to do as you mentioned Many thanks

          1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, AFAIK the general solution that will cope with both stdout and stderr, no matter how much data is expected, requires several threads: - one thread (other than the GUI thread) that reads lines (not ReadToEnd) from stdout - one more thread (other than the GUI thread) that reads lines from stderr - the necessary Control.InvokeRequired and Control.Invoke() stuff to let them access your GUI - and maybe yet another thread that waits for the process to terminate (if that is relevant to your app). And even when you program it according to the documentation (study it!) you will find it difficult to get it to work properly on Win95/98/Me (IIRC that's how it used to be on .NET 1.0/1.1 anyway). :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


            A Offline
            A Offline
            ATC
            wrote on last edited by
            #5

            Hi Luc I will learn more all about your mention Thanks

            1 Reply Last reply
            0
            • D Dave Kreskowiak

              ATC wrote:

              Thread.Sleep(100) s1 = sOut.ReadLine ps1.Kill()

              I love how you wait one tenth of a second, read a single line of it's output, and then kill the process, whether it's doing something or not. Then you continue and try and read lines from a process you already killed off. You cannot do that. The process object must stay running in order for you to read from it's output stream.

              ATC wrote:

              However, if the process does not stop then the ListBox can not update data

              This is because the UI thread is running the code to read the output stream from the process. Until the read operation and filling of the ListBox is done and your code goes idle again, you're controls cannot repaint themselves because the thread is busy doing other things.

              ATC wrote:

              I think because the process takes more priority then the ListBox?)

              Priority has nothing to do with it. Threading does.

              ATC wrote:

              1. Can we modify the above codes so that we can update the ListBox without waiting the process completed?

              You cant read hasn't has been output, so no. You have to wait for the process to finish. If by this statement you mean that you want the control to repaint itself and continue to fill the ListBox with data, you either have to move the code that reads the output stream to another thread, or you have to read the output stream asynchronously.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007

              S Offline
              S Offline
              svanwass
              wrote on last edited by
              #6

              Would background workers fit in this solution? If not, why?

              D 1 Reply Last reply
              0
              • S svanwass

                Would background workers fit in this solution? If not, why?

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

                Sure. BackgroundWorker is just a wrapper for a pool thread. It'll do the job just as easily.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007

                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