线程可靠性:日志文件显示我调用的函数没有随机到达return语句

时间:2013-09-10 11:05:06

标签: vb.net multithreading backgroundworker

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

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

如果我找到新的东西,我会告诉你。

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

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

问题是我的申请不可靠。

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

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

有什么我不做的事吗?这是我第一次使用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

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

1 个答案:

答案 0 :(得分:0)

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

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

抛出异常

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

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

我的问题与BackgroundWorker无关。