在等待的呼叫中忽略了cmd超时

时间:2015-08-11 13:49:57

标签: vb.net asynchronous process cmd async-await

我有以下代码:

Try
Dim startInfo As New ProcessStartInfo("CMD.EXE")
startInfo.WindowStyle = ProcessWindowStyle.Minimized
startInfo.WindowStyle = ProcessWindowStyle.Hidden
startInfo.CreateNoWindow = True
startInfo.UseShellExecute = False
startInfo.RedirectStandardError = True
startInfo.RedirectStandardOutput = True
startInfo.Arguments = "/C " & commandToRun ' run command then quit
Using proc As Process = Process.Start(startInfo)

    Dim stdOutput As Task(Of String)
    Dim stdError As Task(Of String)
    Using stdOutputStreamReader As StreamReader = proc.StandardOutput
        stdOutput = stdOutputStreamReader.ReadToEndAsync()

        Using stdErrorStreamReader As StreamReader = proc.StandardError
            stdError = stdErrorStreamReader.ReadToEndAsync()

            Dim stdOutputString As String = Await stdOutput
            If String.IsNullOrWhiteSpace(stdOutputString) Then
                Return New Tuple(Of String, Boolean)(Await stdError, False)
            Else
                Return New Tuple(Of String, Boolean)(stdOutputString, True)
            End If
        End Using
    End Using
End Using
' Catch error...

要运行的命令是:

commandToRun As String = "timeout 15 > nul & forfiles /S /M *.* /P C:\logs /c ""cmd /c echo @fdate @ftime @path"""

我的问题是,为什么当我到达Await stdOutput(在不到十五秒的时间内)时,命令的第二部分的结果已经存在并等待着我?这就像它忽略了超时。

修改

使用ping 127.0.0.1 -n 15 > nul代替超时工作...但我想知道为什么忽略超时,或者如何在不使用黑客的情况下解决它。

使用/ nobreak标志似乎也没有。

编辑2 - 调用方法:

在后台工作者的DoWork()方法中:

PollAndUpdateLogFiles()

哪个电话

Friend Async Sub PollAndUpdateLogFiles()
    Dim response As String = Await GetLogDataManually()
    ... ' parse to XML, then to an object that is bound by a DataGrid
End Sub

哪个电话

Protected Async Function GetLogDataManually() As Task(Of String)
    Dim commandToRun As String = "timeout 15 > nul & forfiles /S /M *.* /P C:\logs /c ""cmd /c echo @fdate @ftime @path"""
    Dim response As Tuple(Of String, Boolean) = Await HelperMethods.CallCommandAndGetResponse(commandToRun)
    If response.Item2 Then
        Return response.Item1
    ... ' else error
End Function

在问题的顶部调用代码(这是整个方法)

0 个答案:

没有答案