Results 1 to 5 of 5

Thread: [RESOLVED] Batch file output to richtextbox not working anymore.

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Location
    Vermont Republic
    Posts
    11

    Resolved [RESOLVED] Batch file output to richtextbox not working anymore.

    I've been successful at making some code that takes the output from a batch or cmd and puts it on a richtextbox. The user can also send commands. The problem is that I lost some of my work resulting in me rebuilding this part of my program. Can anyone tell me what I'm doing wrong with the rebuild here's the code. I'll check back later.
    Code:
    Public Class Command
        Private WithEvents MyProcess As Process
        Private Delegate Sub AppendOutputTextDelegate(ByVal text As String)
    
        Private Sub Command_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'Start and stop button toggles go here if needed
            If My.Settings.SafeMode = True Then
                OutputComBox.Text = ("Safe Mode Enabled")
                TxtSendCom.Text = ("Safe Mode")
            ElseIf My.Settings.SafeMode = False
                Call OpenPro()
            End If
    
        End Sub
        Public Sub OpenPro()
            MyProcess = New Process
            With MyProcess.StartInfo
                .FileName = (My.Settings.SLoc)
                .UseShellExecute = False
                .CreateNoWindow = True 
                .RedirectStandardInput = True
                .RedirectStandardOutput = True
                .RedirectStandardError = True
            End With
            MyProcess.Start()
            MyProcess.BeginErrorReadLine()
            MyProcess.BeginOutputReadLine()
            Dim output As String = MyProcess.StandardOutput.ReadLine()
            AppendOutputText("Server Started at: " & MyProcess.StartTime.ToString)
            
        End Sub
    
        Private Sub Close_Process()
            If MyProcess.HasExited = False Then
                MyProcess.WaitForExit()
                MyProcess.Close()
            End If
        End Sub
    
        Private Sub TxtSendCom_KeyDown(sender As Object, e As KeyEventArgs) Handles TxtSendCom.KeyDown
            If e.KeyCode = Keys.Enter Then
                ComSendBut.PerformClick()
            End If
        End Sub
    
        Private Sub Command_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
            If My.Settings.SafeMode = False Then
                If MyProcess.HasExited = False Then
                    TxtSendCom.Clear()
                    TxtSendCom.Text = ("stop")
                    ComSendBut.PerformClick()
                    MyProcess.WaitForExit()
                    MyProcess.Close()
                End If
            End If
        End Sub
    
        Private Sub MyProcess_OutputDataReceived(sender As Object, e As DataReceivedEventArgs)
            AppendOutputText(vbCrLf & e.Data)
        End Sub
    
        Private Sub MyProcess_ErrorDataReceived(sender As Object, e As DataReceivedEventArgs)
            MyProcess.StandardInput.Flush()
            AppendOutputText(vbCrLf & e.Data)
            MyProcess.Close()
        End Sub
        Private Sub AppendOutputText(ByVal text As String)
            If OutputComBox.InvokeRequired Then
                Dim myDelegate As New AppendOutputTextDelegate(AddressOf AppendOutputText)
                Invoke(myDelegate, text)
            Else
                OutputComBox.AppendText(text)
            End If
        End Sub
    
        Private Sub ComSendBut_Click(sender As Object, e As EventArgs) Handles ComSendBut.Click
            If My.Settings.SafeMode = False Then
                If TxtSendCom.Text = Nothing Then
                    TxtSendCom.Text = ("Error, You have not typed a command!")
                Else
                    MyProcess.StandardInput.WriteLine(OutputComBox.Text)
                    MyProcess.StandardInput.Flush()
                    TxtSendCom.Text = Nothing
                End If
            Else : OutputComBox.Text = (OutputComBox.Text & vbNewLine & TxtSendCom.Text)
            End If
        End Sub
    End Class
    Last edited by Tiger151; Oct 14th, 2015 at 12:48 PM.

  2. #2
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Batch file output to richtextbox not working anymore.

    maybe its just the
    Code:
    Private Sub load()
    missing its
    Code:
    handles mybase.load
    ?

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Location
    Vermont Republic
    Posts
    11

    Re: Batch file output to richtextbox not working anymore.

    I checked my code and realized that the code i gave you to look at is old here's the newer version:
    Code:
    Public Class Command
        Private WithEvents MyProcess As Process
        Private Delegate Sub AppendOutputTextDelegate(ByVal text As String)
    
        Private Sub Command_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'Start and stop button toggles go here if needed
            If My.Settings.SafeMode = True Then
                OutputComBox.Text = ("Safe Mode Enabled")
                TxtSendCom.Text = ("Safe Mode")
            ElseIf My.Settings.SafeMode = False
                Call OpenPro()
            End If
    
        End Sub
        Public Sub OpenPro()
            MyProcess = New Process
            With MyProcess.StartInfo
                .FileName = (My.Settings.SLoc)
                .UseShellExecute = False
                .CreateNoWindow = True 
                .RedirectStandardInput = True
                .RedirectStandardOutput = True
                .RedirectStandardError = True
            End With
            MyProcess.Start()
            MyProcess.BeginErrorReadLine()
            MyProcess.BeginOutputReadLine()
            Dim output As String = MyProcess.StandardOutput.ReadLine()
            AppendOutputText("Server Started at: " & MyProcess.StartTime.ToString)
            
        End Sub
    
        Private Sub Close_Process()
            If MyProcess.HasExited = False Then
                MyProcess.WaitForExit()
                MyProcess.Close()
            End If
        End Sub
    
        Private Sub TxtSendCom_KeyDown(sender As Object, e As KeyEventArgs) Handles TxtSendCom.KeyDown
            If e.KeyCode = Keys.Enter Then
                ComSendBut.PerformClick()
            End If
        End Sub
    
        Private Sub Command_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
            If My.Settings.SafeMode = False Then
                If MyProcess.HasExited = False Then
                    TxtSendCom.Clear()
                    TxtSendCom.Text = ("stop")
                    ComSendBut.PerformClick()
                    MyProcess.WaitForExit()
                    MyProcess.Close()
                End If
            End If
        End Sub
    
        Private Sub MyProcess_OutputDataReceived(sender As Object, e As DataReceivedEventArgs)
            AppendOutputText(vbCrLf & e.Data)
        End Sub
    
        Private Sub MyProcess_ErrorDataReceived(sender As Object, e As DataReceivedEventArgs)
            MyProcess.StandardInput.Flush()
            AppendOutputText(vbCrLf & e.Data)
            MyProcess.Close()
        End Sub
        Private Sub AppendOutputText(ByVal text As String)
            If OutputComBox.InvokeRequired Then
                Dim myDelegate As New AppendOutputTextDelegate(AddressOf AppendOutputText)
                Invoke(myDelegate, text)
            Else
                OutputComBox.AppendText(text)
            End If
        End Sub
    
        Private Sub ComSendBut_Click(sender As Object, e As EventArgs) Handles ComSendBut.Click
            If My.Settings.SafeMode = False Then
                If TxtSendCom.Text = Nothing Then
                    TxtSendCom.Text = ("Error, You have not typed a command!")
                Else
                    MyProcess.StandardInput.WriteLine(OutputComBox.Text)
                    MyProcess.StandardInput.Flush()
                    TxtSendCom.Text = Nothing
                End If
            Else : OutputComBox.Text = (OutputComBox.Text & vbNewLine & TxtSendCom.Text)
            End If
        End Sub
    End Class
    Last edited by Tiger151; Oct 14th, 2015 at 12:49 PM.

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Batch file output to richtextbox not working anymore.

    You're also missing the Handles clause for MyProcess.ErrorDataReceived and OutputDataReceived.

    Quote Originally Posted by Tiger151 View Post
    Code:
    Private Sub MyProcess_OutputDataReceived(sender As Object, e As DataReceivedEventArgs) Handles MyProcess.OutputDataReceived
    ... 
    
    Private Sub MyProcess_ErrorDataReceived(sender As Object, e As DataReceivedEventArgs) Handles MyProcess.ErrorDataReceived
    ...

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Location
    Vermont Republic
    Posts
    11

    Re: Batch file output to richtextbox not working anymore.

    Yeah I figured that out when the last guy posted thanks anyway. This issue is now resolved.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width