执行几个小时后变量被清空 - CONST 变量的任何替代方法?
Variable being emptied after a few hours of execution - Any alternative to CONST variables?
我正在寻找一些灵感或最佳实践来消除一个非常奇怪的错误...
在下面的代码中,class PDFGenerator 下面一点,我声明了一个 public 名为 strOutPutPath 的变量,因为我在几个函数和完整代码的子部分中使用了它的值...下面它只是一个片段来说明我正在处理的...
在循环生成文件的过程中,strOutPutPath的值随机变为空。它是随机的,它随机发生在 For 循环所涵盖范围内的任何数字,但通常高于 260,并且在循环生成 PDF 文件几个小时后...
因为 strOutPutPath 变为空(我不知道为什么),当迭代(通常高于 260)尝试启动进程并传递一个空的 strOutPutPath 作为参数 /outputdir: 的值,软件挂起,因为 PDFGenerator.exe 收到 /outputdir:[= 的空参数48=] 因此永远不会退出,使主应用程序永远等待它的执行。
我的第一个想法是将 strOutPutPath 声明为 class 中的 public CONST 变量,但是这是不可能的,因为我必须抓住strOutPutPath 的值来自 Main sub.
中的注册表项
此外,我需要在 Main 子范围内处理 strOutPutPath 的内容(下面的代码中未显示),所以这就是为什么我从 Main.
中获取它的原因
我可能正在处理 .NET 错误,或者只是使用了导致 .NET 在应用程序运行几个小时后清空 strOutPutPath 的不良做法。我不知道问题的根本原因。
但我想知道有什么可能的解决方案可以使 strOutPutPath 在 Main 中填充和处理后像 CONST 变量一样工作 子。之后它就不会再改变了,将被六个函数调用。
非常感谢!
Module PDFGenerator
Public strOutPutPath As String
Sub Main()
strOutPutPath = My.Computer.Registry.GetValue(
"HKEY_CURRENT_USER\Software\OutputPath", "Path", Nothing).ToString
For i as Integer = 0 To 1000 -1
GeneratePDFs(i)
Next
End Sub
Private Function GeneratePDFs(ByRef FileID As Integer) As Integer
Dim ExecutableArgs As String = " /outputdir:" + Chr(34) + strOutPutPath +
Chr(34) + " /nodisplay:true /batch:true"
Dim psiMethod2 As New ProcessStartInfo
psiMethod2.UseShellExecute = False
psiMethod2.WorkingDirectory = Path
psiMethod2.WindowStyle = ProcessWindowStyle.Hidden
psiMethod2.FileName = "C:\PDF\PDFGenerator.exe"
psiMethod2.Arguments = ExecutableArgs
Dim procMethod2 As Process = Process.Start(psiMethod2)
procMethod2.WaitForExit()
Return intExitCode
End Function
End Module
进程应该被释放。将 PDFGenerator
设为 class 并尝试以下操作:
注意:您可能需要添加额外的代码才能获得所需的结果。
PDFGenerator.vb
Public Class PDFGenerator
Private _outputPath As String
Public ReadOnly Property OutputPath As String
Get
Return _outputPath
End Get
End Property
Sub New()
_outputPath = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\OutputPath", "Path", Nothing).ToString()
End Sub
Public Function GeneratePDFs(ByRef FileID As Integer) As Integer
'ToDo: add desired code
Dim args As String = String.Format("/outputdir:""{0}"" /nodisplay:true /batch:true", _outputPath)
'System.Diagnostics.Debug.WriteLine("args: " & args)
Dim psInfo As ProcessStartInfo = New ProcessStartInfo()
'set properties
psInfo.Arguments = args 'arguments
psInfo.CreateNoWindow = True 'don't create a window
psInfo.FileName = "C:\PDF\PDFGenerator.exe"
psInfo.RedirectStandardError = True 'redirect standard Error
psInfo.RedirectStandardOutput = True 'redirect standard output
psInfo.RedirectStandardInput = False
psInfo.UseShellExecute = False 'If True, uses 'ShellExecute'; if false, uses 'CreateProcess'
psInfo.WindowStyle = ProcessWindowStyle.Hidden
'psInfo.WorkingDirectory = ""
psInfo.ErrorDialog = False
Using p As Process = New Process() With {.EnableRaisingEvents = True, .StartInfo = psInfo}
'subscribe to events (add event handlers)
AddHandler p.ErrorDataReceived, AddressOf Process_ErrorDataReceived
AddHandler p.OutputDataReceived, AddressOf Process_OutputDataReceived
'start process
p.Start()
p.BeginErrorReadLine() 'begin async reading for standard error
p.BeginOutputReadLine() 'begin async reading for standard output
'waits until the process is finished before continuing
p.WaitForExit()
'unsubscribe from events (remove event handlers)
RemoveHandler p.ErrorDataReceived, AddressOf Process_ErrorDataReceived
RemoveHandler p.OutputDataReceived, AddressOf Process_OutputDataReceived
'return exit code
Return p.ExitCode
End Using
End Function
Private Sub Process_ErrorDataReceived(sender As Object, e As DataReceivedEventArgs)
'ToDo: add desired code
If Not String.IsNullOrEmpty(e.Data) Then
'ToDo: add desired code
System.Diagnostics.Debug.WriteLine("Process_ErrorDataReceived: " & e.Data)
End If
End Sub
Private Sub Process_OutputDataReceived(sender As Object, e As DataReceivedEventArgs)
'ToDo: add desired code
If Not String.IsNullOrEmpty(e.Data) Then
'ToDo: add desired code
System.Diagnostics.Debug.WriteLine("Process_OutputDataReceived: " & e.Data)
End If
End Sub
End Class
我正在寻找一些灵感或最佳实践来消除一个非常奇怪的错误...
在下面的代码中,class PDFGenerator 下面一点,我声明了一个 public 名为 strOutPutPath 的变量,因为我在几个函数和完整代码的子部分中使用了它的值...下面它只是一个片段来说明我正在处理的...
在循环生成文件的过程中,strOutPutPath的值随机变为空。它是随机的,它随机发生在 For 循环所涵盖范围内的任何数字,但通常高于 260,并且在循环生成 PDF 文件几个小时后...
因为 strOutPutPath 变为空(我不知道为什么),当迭代(通常高于 260)尝试启动进程并传递一个空的 strOutPutPath 作为参数 /outputdir: 的值,软件挂起,因为 PDFGenerator.exe 收到 /outputdir:[= 的空参数48=] 因此永远不会退出,使主应用程序永远等待它的执行。
我的第一个想法是将 strOutPutPath 声明为 class 中的 public CONST 变量,但是这是不可能的,因为我必须抓住strOutPutPath 的值来自 Main sub.
中的注册表项此外,我需要在 Main 子范围内处理 strOutPutPath 的内容(下面的代码中未显示),所以这就是为什么我从 Main.
中获取它的原因我可能正在处理 .NET 错误,或者只是使用了导致 .NET 在应用程序运行几个小时后清空 strOutPutPath 的不良做法。我不知道问题的根本原因。
但我想知道有什么可能的解决方案可以使 strOutPutPath 在 Main 中填充和处理后像 CONST 变量一样工作 子。之后它就不会再改变了,将被六个函数调用。
非常感谢!
Module PDFGenerator
Public strOutPutPath As String
Sub Main()
strOutPutPath = My.Computer.Registry.GetValue(
"HKEY_CURRENT_USER\Software\OutputPath", "Path", Nothing).ToString
For i as Integer = 0 To 1000 -1
GeneratePDFs(i)
Next
End Sub
Private Function GeneratePDFs(ByRef FileID As Integer) As Integer
Dim ExecutableArgs As String = " /outputdir:" + Chr(34) + strOutPutPath +
Chr(34) + " /nodisplay:true /batch:true"
Dim psiMethod2 As New ProcessStartInfo
psiMethod2.UseShellExecute = False
psiMethod2.WorkingDirectory = Path
psiMethod2.WindowStyle = ProcessWindowStyle.Hidden
psiMethod2.FileName = "C:\PDF\PDFGenerator.exe"
psiMethod2.Arguments = ExecutableArgs
Dim procMethod2 As Process = Process.Start(psiMethod2)
procMethod2.WaitForExit()
Return intExitCode
End Function
End Module
进程应该被释放。将 PDFGenerator
设为 class 并尝试以下操作:
注意:您可能需要添加额外的代码才能获得所需的结果。
PDFGenerator.vb
Public Class PDFGenerator
Private _outputPath As String
Public ReadOnly Property OutputPath As String
Get
Return _outputPath
End Get
End Property
Sub New()
_outputPath = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\OutputPath", "Path", Nothing).ToString()
End Sub
Public Function GeneratePDFs(ByRef FileID As Integer) As Integer
'ToDo: add desired code
Dim args As String = String.Format("/outputdir:""{0}"" /nodisplay:true /batch:true", _outputPath)
'System.Diagnostics.Debug.WriteLine("args: " & args)
Dim psInfo As ProcessStartInfo = New ProcessStartInfo()
'set properties
psInfo.Arguments = args 'arguments
psInfo.CreateNoWindow = True 'don't create a window
psInfo.FileName = "C:\PDF\PDFGenerator.exe"
psInfo.RedirectStandardError = True 'redirect standard Error
psInfo.RedirectStandardOutput = True 'redirect standard output
psInfo.RedirectStandardInput = False
psInfo.UseShellExecute = False 'If True, uses 'ShellExecute'; if false, uses 'CreateProcess'
psInfo.WindowStyle = ProcessWindowStyle.Hidden
'psInfo.WorkingDirectory = ""
psInfo.ErrorDialog = False
Using p As Process = New Process() With {.EnableRaisingEvents = True, .StartInfo = psInfo}
'subscribe to events (add event handlers)
AddHandler p.ErrorDataReceived, AddressOf Process_ErrorDataReceived
AddHandler p.OutputDataReceived, AddressOf Process_OutputDataReceived
'start process
p.Start()
p.BeginErrorReadLine() 'begin async reading for standard error
p.BeginOutputReadLine() 'begin async reading for standard output
'waits until the process is finished before continuing
p.WaitForExit()
'unsubscribe from events (remove event handlers)
RemoveHandler p.ErrorDataReceived, AddressOf Process_ErrorDataReceived
RemoveHandler p.OutputDataReceived, AddressOf Process_OutputDataReceived
'return exit code
Return p.ExitCode
End Using
End Function
Private Sub Process_ErrorDataReceived(sender As Object, e As DataReceivedEventArgs)
'ToDo: add desired code
If Not String.IsNullOrEmpty(e.Data) Then
'ToDo: add desired code
System.Diagnostics.Debug.WriteLine("Process_ErrorDataReceived: " & e.Data)
End If
End Sub
Private Sub Process_OutputDataReceived(sender As Object, e As DataReceivedEventArgs)
'ToDo: add desired code
If Not String.IsNullOrEmpty(e.Data) Then
'ToDo: add desired code
System.Diagnostics.Debug.WriteLine("Process_OutputDataReceived: " & e.Data)
End If
End Sub
End Class