作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编辑:我找到了原因,因为函数语句包装在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.
关于vb.net - 线程可靠性: log file shows function that I call a lot did not reach return statement randomly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18717246/
我是一名优秀的程序员,十分优秀!