【问题标题】:Execute command promt process asnyc and get result执行命令提示符进程异步并获取结果
【发布时间】:2011-10-18 08:57:47
【问题描述】:

我需要异步执行 commandpromt 进程并获取执行的输出。我目前有这个代码

Public Function ExecuteCommandSync(ByVal command As Object) As String
    Dim result As String = Nothing
    Try
        Dim procStartInfo As New System.Diagnostics.ProcessStartInfo("cmd", "/c " & Convert.ToString(command))
        procStartInfo.RedirectStandardOutput = True
        procStartInfo.UseShellExecute = False
        procStartInfo.CreateNoWindow = True
        Dim proc As New System.Diagnostics.Process()
        proc.StartInfo = procStartInfo
        proc.Start()
        result = proc.StandardOutput.ReadToEnd()
        Console.WriteLine(result)
    Catch objException As Exception
    End Try
    Return result
End Function

请帮助我在不使用线程的情况下将其转换为异步。这可能吗?

谢谢

【问题讨论】:

    标签: .net vb.net winforms asynchronous process


    【解决方案1】:

    下面是一个实现我相信你正在寻找的课程。

    该进程已经在异步运行,我相信您正在寻找的是事件驱动和隐藏的。 您是出于某种原因特别想要阻塞调用,还是害怕线程?

    如果我不在基地并且您希望它阻止,请告诉我我们也可以这样做,但我无法想象为什么。

    它本质上允许您创建一个 cmd shell 并与它进行隐形交互。

    #Region " Imports "
    
    Imports System.Threading
    Imports System.ComponentModel
    
    #End Region
    
    Namespace Common
    
        Public Class CmdShell
    
    #Region " Variables "
    
            Private WithEvents ShellProcess As Process
    
    #End Region
    
    #Region " Events "
    
            ''' <summary>
            ''' Event indicating an asyc read of the command process's StdOut pipe has occured.
            ''' </summary>
            Public Event DataReceived As EventHandler(Of CmdShellDataReceivedEventArgs)
    
    #End Region
    
    #Region " Public Methods "
    
            Public Sub New()
                ThreadPool.QueueUserWorkItem(AddressOf ShellLoop, Nothing)
                Do Until Not ShellProcess Is Nothing : Loop
            End Sub
    
            ''' <param name="Value">String value to write to the StdIn pipe of the command process, (CRLF not required).</param>
            Public Sub Write(ByVal value As String)
                ShellProcess.StandardInput.WriteLine(value)
            End Sub
    
    #End Region
    
    #Region " Private Methods "
    
            Private Sub ShellLoop(ByVal state As Object)
                Try
                    Dim SI As New ProcessStartInfo("cmd.exe")
                    With SI
                        .Arguments = "/k"
                        .RedirectStandardInput = True
                        .RedirectStandardOutput = True
                        .RedirectStandardError = True
                        .UseShellExecute = False
                        .CreateNoWindow = True
                        .WorkingDirectory = Environ("windir")
                    End With
                    Try
                        ShellProcess = Process.Start(SI)
                        With ShellProcess
                            .BeginOutputReadLine()
                            .BeginErrorReadLine()
                            .WaitForExit()
                        End With
                    Catch ex As Exception
                        With ex
                            Trace.WriteLine(.Message)
                            Trace.WriteLine(.Source)
                            Trace.WriteLine(.StackTrace)
                        End With
                    End Try
                Catch ex As Exception
                    With ex
                        Trace.WriteLine(.Message)
                        Trace.WriteLine(.Source)
                        Trace.WriteLine(.StackTrace)
                    End With
                End Try
            End Sub
    
            Private Sub ShellProcess_ErrorDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles ShellProcess.ErrorDataReceived
                If Not e.Data Is Nothing Then RaiseEvent DataReceived(Me, New CmdShellDataReceivedEventArgs(e.Data))
            End Sub
    
            Private Sub ShellProcess_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles ShellProcess.OutputDataReceived
                If Not e.Data Is Nothing Then RaiseEvent DataReceived(Me, New CmdShellDataReceivedEventArgs(e.Data & Environment.NewLine))
            End Sub
    
    #End Region
    
        End Class
    
        <EditorBrowsable(EditorBrowsableState.Never)> _
           Public Class CmdShellDataReceivedEventArgs : Inherits EventArgs
            Private _Value As String
    
            Public Sub New(ByVal value As String)
                _Value = value
            End Sub
    
            Public ReadOnly Property Value() As String
                Get
                    Return _Value
                End Get
            End Property
    
        End Class
    
    End Namespace
    

    【讨论】:

      猜你喜欢
      • 2011-11-03
      • 2016-03-21
      • 1970-01-01
      • 2014-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      • 1970-01-01
      相关资源
      最近更新 更多