Jimi2000

ProcessStartRedirectOutput

Jul 9th, 2018
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.77 KB | None | 0 0
  1. '/----------------------  Form designer ---------------------/'
  2. '/-------------------  Visual Studio 15.7.4  ----------------/'
  3.  
  4. <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
  5. Partial Class Form1
  6.     Inherits System.Windows.Forms.Form
  7.  
  8.     'Form overrides dispose to clean up the component list.
  9.     <System.Diagnostics.DebuggerNonUserCode()> _
  10.     Protected Overrides Sub Dispose(ByVal disposing As Boolean)
  11.         Try
  12.             If disposing AndAlso components IsNot Nothing Then
  13.                 components.Dispose()
  14.             End If
  15.         Finally
  16.             MyBase.Dispose(disposing)
  17.         End Try
  18.     End Sub
  19.  
  20.     'Required by the Windows Form Designer
  21.     Private components As System.ComponentModel.IContainer
  22.  
  23.     'NOTE: The following procedure is required by the Windows Form Designer
  24.     'It can be modified using the Windows Form Designer.  
  25.     'Do not modify it using the code editor.
  26.     <System.Diagnostics.DebuggerStepThrough()> _
  27.     Private Sub InitializeComponent()
  28.         Me.RichTextBox1 = New System.Windows.Forms.RichTextBox()
  29.         Me.Button1 = New System.Windows.Forms.Button()
  30.         Me.lbl_status = New System.Windows.Forms.Label()
  31.         Me.SuspendLayout()
  32.         '
  33.         'RichTextBox1
  34.         '
  35.         Me.RichTextBox1.BackColor = System.Drawing.Color.FromArgb(CType(CType(64, Byte), Integer), CType(CType(64, Byte), Integer), CType(CType(64, Byte), Integer))
  36.         Me.RichTextBox1.ForeColor = System.Drawing.Color.White
  37.         Me.RichTextBox1.Location = New System.Drawing.Point(12, 76)
  38.         Me.RichTextBox1.Name = "RichTextBox1"
  39.         Me.RichTextBox1.Size = New System.Drawing.Size(457, 153)
  40.         Me.RichTextBox1.TabIndex = 0
  41.         Me.RichTextBox1.Text = ""
  42.         '
  43.         'Button1
  44.         '
  45.         Me.Button1.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(CType(CType(128, Byte), Integer), CType(CType(64, Byte), Integer), CType(CType(0, Byte), Integer))
  46.         Me.Button1.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(CType(CType(192, Byte), Integer), CType(CType(64, Byte), Integer), CType(CType(0, Byte), Integer))
  47.         Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat
  48.         Me.Button1.Font = New System.Drawing.Font("Segoe UI", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  49.         Me.Button1.ForeColor = System.Drawing.Color.White
  50.         Me.Button1.Location = New System.Drawing.Point(12, 24)
  51.         Me.Button1.Name = "Button1"
  52.         Me.Button1.Size = New System.Drawing.Size(88, 35)
  53.         Me.Button1.TabIndex = 1
  54.         Me.Button1.Text = "Start"
  55.         Me.Button1.UseVisualStyleBackColor = True
  56.         '
  57.         'lbl_status
  58.         '
  59.         Me.lbl_status.Font = New System.Drawing.Font("Segoe UI", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  60.         Me.lbl_status.ForeColor = System.Drawing.Color.White
  61.         Me.lbl_status.Location = New System.Drawing.Point(244, 25)
  62.         Me.lbl_status.Name = "lbl_status"
  63.         Me.lbl_status.Size = New System.Drawing.Size(225, 32)
  64.         Me.lbl_status.TabIndex = 2
  65.         Me.lbl_status.TextAlign = System.Drawing.ContentAlignment.MiddleRight
  66.         '
  67.         'Form1
  68.         '
  69.         Me.AutoScaleDimensions = New System.Drawing.SizeF(96.0!, 96.0!)
  70.         Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi
  71.         Me.BackColor = System.Drawing.Color.FromArgb(CType(CType(32, Byte), Integer), CType(CType(32, Byte), Integer), CType(CType(32, Byte), Integer))
  72.         Me.ClientSize = New System.Drawing.Size(481, 244)
  73.         Me.Controls.Add(Me.lbl_status)
  74.         Me.Controls.Add(Me.Button1)
  75.         Me.Controls.Add(Me.RichTextBox1)
  76.         Me.ForeColor = System.Drawing.Color.Black
  77.         Me.Name = "Form1"
  78.         Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
  79.         Me.Text = "Form1"
  80.         Me.ResumeLayout(False)
  81.  
  82.     End Sub
  83.  
  84.     Friend WithEvents RichTextBox1 As RichTextBox
  85.     Friend WithEvents Button1 As Button
  86.     Friend WithEvents lbl_status As Label
  87. End Class
  88.  
  89. '/-----------------------------  Code ----------------------------------/'
  90.  
  91. Imports System.Diagnostics
  92. Imports System.IO
  93.  
  94. 'This code uses the Process.SynchronizingObject property, thus the it doesn't really need
  95. 'MethodInvoker. It's added anyway (in the Event Handler) to see how it could work.
  96. 'It doesn't modify the Process.OutputDataReceived results in any way.
  97. 'Delete .SynchronizingObject = Me from the Process contructor to activate it
  98.  
  99. Public Class Form1
  100.  
  101.     Private myappthing_proc As Process
  102.     Private myappthing_proc_state As Boolean
  103.     Private ProcessExitCode As Integer = -1
  104.  
  105.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  106.         StartProcess()
  107.     End Sub
  108.  
  109.     Private Sub StartProcess()
  110.  
  111.         lbl_status.Text = "Status: Running"
  112.  
  113.         Dim MyStartInfo As New ProcessStartInfo()
  114.         MyStartInfo.FileName = "tracert.exe"
  115.         MyStartInfo.Arguments = "www.microsoft.com"
  116.         MyStartInfo.WorkingDirectory = Path.Combine(Application.StartupPath, "backend")
  117.         MyStartInfo.RedirectStandardError = True
  118.         MyStartInfo.RedirectStandardOutput = True
  119.         MyStartInfo.UseShellExecute = False
  120.         MyStartInfo.CreateNoWindow = True
  121.         MyStartInfo.WindowStyle = ProcessWindowStyle.Hidden
  122.         myappthing_proc = New Process() With {
  123.             .EnableRaisingEvents = True,
  124.             .StartInfo = MyStartInfo,
  125.             .SynchronizingObject = Me
  126.         }
  127.  
  128.         myappthing_proc_state = True
  129.         myappthing_proc.Start()
  130.         myappthing_proc.BeginErrorReadLine()
  131.         myappthing_proc.BeginOutputReadLine()
  132.  
  133.         AddHandler myappthing_proc.OutputDataReceived,
  134.             Sub(sender As Object, e As DataReceivedEventArgs)
  135.                 If e.Data IsNot Nothing Then
  136.                     Dim output As String = e.Data.Replace("[0m", "")
  137.                     If InvokeRequired Then
  138.                         Dim mi As MethodInvoker =
  139.                             Sub()
  140.                                 RichTextBox1.AppendText(output + Environment.NewLine)
  141.                                 RichTextBox1.ScrollToCaret()
  142.                             End Sub
  143.                         Invoke(mi)
  144.                     Else
  145.                         RichTextBox1.AppendText(output + Environment.NewLine)
  146.                         RichTextBox1.ScrollToCaret()
  147.                     End If
  148.                 End If
  149.             End Sub
  150.  
  151.         AddHandler myappthing_proc.ErrorDataReceived,
  152.             Sub(sender As Object, e As DataReceivedEventArgs)
  153.                 If e.Data IsNot Nothing Then
  154.                     Dim output As String = e.Data.Replace("[0m", "")
  155.                     If InvokeRequired Then
  156.                         Dim mi As MethodInvoker =
  157.                             Sub()
  158.                                 RichTextBox1.AppendText(output  + Environment.NewLine)
  159.                                 RichTextBox1.ScrollToCaret()
  160.                             End Sub
  161.                         Invoke(mi)
  162.                     Else
  163.                          RichTextBox1.AppendText(output + Environment.NewLine)
  164.                         RichTextBox1.ScrollToCaret()
  165.                     End If
  166.                 End If
  167.             End Sub
  168.  
  169.         AddHandler myappthing_proc.Exited,
  170.             Sub(source As Object, ev As EventArgs)
  171.                 ProcessExitCode = myappthing_proc.ExitCode
  172.                 myappthing_proc.Close()
  173.                 If Not (IsNothing(myappthing_proc)) Then
  174.                     myappthing_proc.Dispose()
  175.                 End If
  176.                 lbl_status.Text = "Status: Exited"
  177.                 myappthing_proc_state = False
  178.             End Sub
  179.     End Sub
  180. End Class
Advertisement
Add Comment
Please, Sign In to add comment