gpt4 book ai didi

vb.net - 线程可靠性: log file shows function that I call a lot did not reach return statement randomly

转载 作者:行者123 更新时间:2023-12-03 13:13:00 25 4
gpt4 key购买 nike

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

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

如果我发现新东西,我会通知您。

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

所有这些调用都是在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

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

最佳答案

我在代码中找到了该错误。该行:

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

抛出异常
System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.

因为命令的运行时间各不相同,并且有时在“。”之后没有4位数字。

我的问题与BackgroundWorker无关。

关于vb.net - 线程可靠性: log file shows function that I call a lot did not reach return statement randomly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18717246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com