执行命令PROMT过程asnyc并得到结果 [英] Execute command promt process asnyc and get result
问题描述
我需要执行commandpromt过程异步,并得到执行的输出。目前,我有这个code
I need to execute commandpromt process async and get the output of the execution. i currently have this code
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
请帮我在这转换,而无需使用一个线程异步。这可能吗?
Please help me on converting this to async without using a thread. it this possible?
感谢
推荐答案
下面是实现了类,我相信你在找什么。
Below is a class that achieves I believe what you are looking for.
该进程已经运行异步的,我相信你正在寻找的是事件驱动的和隐藏。 你特别想要一个阻塞调用由于某种原因,还是吓了线程的?
The process is already running async, I believe what you are looking for is event driven and hidden. Do you specifically want a blocking call for some reason, or scared of the threading?
如果我是关闭基地,而你希望它挡住,让我知道我们可以做到这一点,我不能想象为什么虽然。
If I am off base and you want it to block, let me know we can do that too, I cannot imagine why though.
这基本上可以让你创建一个命令shell并与之交互无形。
It essentially allows you to create a cmd shell and interact with it invisibly.
#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
这篇关于执行命令PROMT过程asnyc并得到结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!