|
tics.Process = New System.Diagnostics.Process Dim psI As New System.Diagnostics.ProcessStartInfo(System.Environment.GetEnvironmentVariable("ComSpec")) #End Region Private Sub frmCmdExcute_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load p = New System.Diagnostics.Process ''''"cmd.exe"为nt的命令行程序 psI = New System.Diagnostics.ProcessStartInfo("cmd.exe") psI.UseShellExecute = False psI.RedirectStandardInput = True psI.RedirectStandardOutput = True psI.RedirectStandardError = True psI.CreateNoWindow = True p.StartInfo = psI p.Start() sw = p.StandardInput sr.stream = p.StandardOutput err.stream = p.StandardError sw.AutoFlush = True sr.stream.BaseStream.BeginRead(sr.bytes, 0, 1024, New AsyncCallback(AddressOf CBstream), sr) err.stream.BaseStream.BeginRead(err.bytes, 0, 1024, New AsyncCallback(AddressOf CBstream), err) End Sub Private Sub frmCmdExcute_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed p.Close() If Not sw Is Nothing Then sw.Close() End If If Not sr Is Nothing Then sr.stream.Close() End If If Not err Is Nothing Then err.stream.Close() End If End Sub Private Sub btClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btClear.Click Me.tbResult.Text = String.Empty End Sub Private Sub btnExcute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExcute.Click Me.CmdExcute() End Sub Private Sub CmdExcute() Try Me.Cursor = System.Windows.Forms.Cursors.WaitCursor If Me.tbComText.Text <> "" Then sw.WriteLine(Me.tbComText.Text) Else sw.WriteLine("Dir") End If Me.Cursor = System.Windows.Forms.Cursors.Default ''''向combobox中添加元素 Me.tbComText.Items.Add(Me.tbComText.Text) Me.tbComText.SelectionStart = 0 Me.tbComText.SelectionLength = Me.tbComText.Text.Length Me.tbComText.Focus() Catch ex As Exception MsgBox("CmdExcute--" & ex.ToString) End Try End Sub Private Sub AppText(ByVal strAdd As String) Me.tbResult.Text &= strAdd End Sub ''''回调函数 ''''为了保证执行invoke方法的线程顺序,使用了MethodImpl <MethodImpl(MethodImplOptions.Synchronized)> _ Sub CBstream(ByVal s As IAsyncResult) Try Dim t As MyStreamReader = CType(s.AsyncState, MyStreamReader) If t.stream.BaseStream Is Nothing Then Exit Sub End If Dim i As Integer = t.stream.BaseStream.EndRead(s) Dim strReceive As String = System.Text.Encoding.Default.GetString(t.bytes, 0, i) Me.Invoke(New TextAddHandler(AddressOf AppText), New Object() {strReceive}) t.stream.BaseStream.BeginRead(t.bytes, 0, 1024, New AsyncCallback(AddressOf CBstream), t) Catch ex As Exception MsgBox("CBstream--" & ex.ToString) End Try End Sub Friend Class MyStreamReader Public stream As IO.StreamReader Public bytes(1024) As Byte Public Sub New() End Sub End Class End Class
上一页 [1] [2] [系统软件]windows下Apache+php+mysql的安装与配置图解 [操作系统]在Windows中玩转Linux操作系统 [操作系统]死马还当活马医:6种方法挽救Windows系统 [聊天工具]四大更新 Windows Live Msn 8.1评测 [聊天工具]Windows Live Messenger最新0683版亮相_联络工具_… [聊天工具]Windows Live Mail招人爱的N个理由_联络工具_Wind… [聊天工具]Windows Live Mail Desktop多图欣赏_联络工具_Win… [聊天工具]OE老了 微软开发新邮件客户端取而代之_联络工具 [聊天工具]Windows Live Messenger中文版试用报告(一)__天极… [聊天工具]Windows Live Messenger 8 Beta1高清图赏__天极Ye…
|