2

编辑:我找到了原因,因为函数语句包含在 try/catch 子句中,函数无法到达无法到达退出代码将被记录的语句。

我现在也在 catch 子句中登录。

如果我发现新的东西,我会告诉你。

我正在 VB.NET 中创建一个 UI 应用程序,它创建进程来运行一些命令行应用程序并获取它们返回的字符串。

所有这些调用都是在 BackgroundWorker 线程中进行的。

问题是我的应用程序不可靠。

有时它工作正常,有时函数没有到达返回语句。

我看到这一点是因为我在函数返回之前将信息记录在文本文件中。

有什么我没有做的吗?这是我第一次使用后台工作者。

如果您需要更多信息,我很乐意为您提供。

编辑:我不在 UI 中使用的 ExecuteCommand 函数返回的信息,我用它创建了一个 Excel 文件。编辑:我调用的函数

Public Function ExecuteCommand(ByVal Command As String) As String
    Dim bw As BackgroundWorker = MainForm.BackgroundWorker1
    RuntimeHelpers.PrepareConstrainedRegions()
    Try
        Dim result As String = String.Empty
        Dim startTime As Date
        Dim endTime As Date
        Dim procStartInfo As New System.Diagnostics.ProcessStartInfo("cmd", "/c " & Command)


        objLog.LogAction("[" & Format(Now, "HH:mm:ss") & "]" & " " & Command.ToString)
        startTime = Now
        ' The following commands are needed to redirect the standard output.
        ' This means that it will be redirected to the Process.StandardOutput StreamReader.
        procStartInfo.RedirectStandardOutput = True
        procStartInfo.RedirectStandardError = True
        procStartInfo.UseShellExecute = False
        ' Do not create the black window.
        procStartInfo.CreateNoWindow = True
        ' Now we create a process, assign its ProcessStartInfo and start it
        Dim proc As New System.Diagnostics.Process()

        AddHandler proc.OutputDataReceived, AddressOf StdOutputDataHandler
        sStdOutput = New StringBuilder()

        proc.StartInfo = procStartInfo
        proc.Start()

        proc.BeginOutputReadLine()





        Dim exitCode As Integer
        proc.WaitForExit()
        exitCode = proc.ExitCode




        Dim rsStdOut As String = String.Empty
        If sStdOutput.Length > 0 Then
            rsStdOut = sStdOutput.ToString
        End If


        proc.Close()
        proc = Nothing
        endTime = Now
        Dim elapsed As String
        elapsed = (endTime - startTime).TotalSeconds.ToString
        elapsed = elapsed.Substring(0, elapsed.IndexOf(".") + 4)
        objLog.LogAction("[" & Format(Now, "HH:mm:ss") & "]" & " exit code: " & exitCode.ToString() + vbCrLf + vbTab + "Command execution took " + elapsed + " seconds" + vbCrLf)


        If exitCode <> 0 Then
            result = ERROR_MESSAGE
        Else
            result = rsStdOut
        End If


        result = result.Trim()
        If result.Length > 1 Then
            'result = result.Substring(0, result.Length - 1)
        End If

        'result = Regex.Replace(result, "\n", RESULT_SPLITER)

        Return result
    Catch ex As Exception
        Return ERROR_MESSAGE
    End Try


End Function

和日志文件摘录:

[13:44:05] si viewproject -P project -Y --projectRevision 1.4
[13:44:05] si viewproject -P project -Y --projectRevision 1.5
[13:44:05] si viewproject -P project -Y --projectRevision 1.6
[13:44:06] exit code: 0
Command execution took 0.079 seconds

[13:44:06] si viewproject -P project -Y --projectRevision 1.7
[13:44:06] exit code: 0
Command execution took 0.082 seconds

在代表命令的每一行之后应该有一个退出代码

4

1 回答 1

0

我在我的代码中发现了这个错误。该行:

elapsed = elapsed.Substring(0, elapsed.IndexOf(".") + 4)

抛出异常

System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.

因为命令的运行时间不同,有时“。”后面没有 4 位数字。

我的问题与 BackgroundWorker 无关。

于 2013-09-10T13:37:21.630 回答