Redirecting console output in realtime?

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

    Redirecting console output in realtime?

    Hello,

    I have a problem with a DOS EXE that is called by a .Net Winforms
    application. I need to redirect the console output into a textbox, but
    this should happen in real time, so when new output is written to the
    redirected StandardOutput-Reader, I want to catch it immediately and
    add it the my textbox.

    Now, I have tried everything from threads to invokes, but every time i
    call the read/readline/readtoend function in the thread, it suspends at
    this point. After the process is finished, my thread goes on and the
    output is written to the textbox.

    Does anybody have an idea how i can make the thread run the entire
    time, without stopping after the first access of standardoutput?

    I should add that I use VB 2003, so I am not able to use any of the new
    2005 classes.

    Here are some code snippets:
    (the following code lies in an extra class, outside of the GUI)

    'Called from the Form when "start"-button is clicked
    Public Sub Start()
    'See below for exact code of createProcess()
    myProc = createProcess()
    myProc.Start()

    'New Thread for catching the StandardOutput in realtime
    myThread = New Threading.Threa d(AddressOf startCatch)
    myThread.Name = "ConsoleCatcher "
    myThread.IsBack ground = True

    myThread.Start( )

    End Sub

    'Creates a Process with appropriate settings
    Private Function createProcess() As Process
    Dim objP As New Process
    objP.StartInfo. UseShellExecute = False
    objP.StartInfo. RedirectStandar dOutput = True
    objP.StartInfo. RedirectStandar dError = True
    objP.StartInfo. CreateNoWindow = True
    objP.StartInfo. WorkingDirector y = Application.Sta rtupPath

    objP.StartInfo. FileName = myFile
    objP.StartInfo. Arguments = myArgs

    Return objP
    End Function


    'That's what happens in the output-catching function
    Private Sub startCatch()
    Try
    Dim strLine As String

    Do While Not myProc Is Nothing
    'The Thread stops while executing the line below for the
    first time.
    'It continues normally after the Process belonging to the
    StandardOutput has finished...
    strLine = myProc.Standard Output.ReadLine ()
    If strLine <> "" And strLine <> Nothing Then

    'Event caught by the Form
    RaiseEvent ConsoleOut(Me, New
    DataReceivedEve ntArgs(strLine) )
    End If

    Loop

    'See if there's anything left
    strLine = myReader.ReadTo End
    If strLine <> Nothing And strLine <> "" Then
    RaiseEvent ConsoleOut(Me, New
    DataReceivedEve ntArgs(strLine) )
    End If
    Catch
    End Try

    End Sub


    Any help is highly appreciated. Thanks alot! :-)

    Bye,
    Markus

  • Herfried K. Wagner [MVP]

    #2
    Re: Redirecting console output in realtime?

    "Markus S." <sneets@virtual sneets.de> schrieb:[color=blue]
    > I have a problem with a DOS EXE that is called by a .Net Winforms
    > application. I need to redirect the console output into a textbox, but
    > this should happen in real time, so when new output is written to the
    > redirected StandardOutput-Reader, I want to catch it immediately and
    > add it the my textbox.
    >
    > Now, I have tried everything from threads to invokes, but every time i
    > call the read/readline/readtoend function in the thread, it suspends at
    > this point. After the process is finished, my thread goes on and the
    > output is written to the textbox.[/color]

    <URL:http://www.palmbytes.d e/content/dotnet/console.htm>
    <URL:http://dotnet.mvps.org/dotnet/samples/misc/#RedirectConsol e>

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Redirecting console output in realtime?

      Markus,

      Any problem that I show you this sample instead of looking to your code (I
      made it in this way now, tested it however changed some things for the page
      so watch errors because of that changing).



      I hope this helps,

      Cor


      Comment

      • Markus S.

        #4
        Re: Redirecting console output in realtime?

        Hi again,

        the first example from
        http://www.palmbytes.de/content/dotnet/console.htm is almost the same I
        did. and it behaves the way my app does :-)

        Maybe I have to specify my problem more exactly.

        I am writing a .net frontend for cabarc.exe which created cab archives.

        now, let's assume I call the exe with a filelist of 20 files. when i
        use the standard console output, the screen is updated after every file
        (i can watch the progress in real time, as i get a line saying "file x
        added to archive"). when i redirect the output and start cabarc, I
        won't get a response from standardoutput. readline until the exe has
        finished running...
        Slowly it makes me belive that this behavior is just the way the
        process class works (waiting until the underlying exe-process is
        finished and then redirecting the whole bunch of lines in one step)...

        or is there any workaround for this maybe?

        But thanks already for the help everybody!

        Comment

        • Cor Ligthert [MVP]

          #5
          Re: Redirecting console output in realtime?

          Markus,

          Did you beside that palmbytes sample as well tried the sample I made for
          you?

          Cor


          Comment

          • WolverineSoft
            New Member
            • May 2006
            • 1

            #6
            Api

            Hello Markus,
            hope to find someone with the same mine problem ;)

            First of all, i want to tell you of i'm searching for an answer for a long , very long time....

            I tryed, like you, to use Cabarc, but my first purpose is to use the redirection in realtime with GoldenHawk DAO burning technology...

            As you know most simple software is able to send realtime output to the VB redirector. Most people try to help us in this wrong way. They don't know the existing of some particular dos.exe softwares which not show the output immediately, or don't show it at all untill the end...(cabarc is a valid example)

            Please sorry for my bad english, i'm italian...

            Anyway i coded a special software which is able to do it...

            Simple: i builded a Console application which launch a DOS.exe software and read its output realtime by directly reading the screen.....ther e are some useful APIs to do it.... and it works 100%..(also in hidden mode)

            My software SubSonic Music Burner does it perfectly...

            The only issue is on how to pass data beetween the Console Application 8Not the EXE, but the Console application i builded to read the EXE output) and the main visual basic project, the form....
            I'm using right now the windows registry, but i hope to really be able to find a better way, because this way is frustrating and a little slow....

            My email is stefano@subsoni c.it and you can find me here if you need the code. in exchange i hope you will let me know if you will find a real way to get the output realtime with the simple standard redirection....

            Please Note, i found the new and awesome new feature within visual basic 2005 express which read data asyncronously from the output. This is cool but unfortunately the unable to get it realtime remain inalterated :D ...
            Another cool info is of the StandarError is able to show it realtime....i think it have a different PIPE buffer size...Anyway at this time only the LAME Mp3 tools is using the standard error to show realtime the progress...

            I hope i wiil able to help you exactly like you will be able to help me in the future...

            Stefano

            Comment

            Working...