我构建了一个监视一组使用VB .Net Framework 4构建的Forms程序的服务。这是一个多实例表单程序,其中文件名相应地重命名。如果表单程序未运行,则该过程启动该程序:
Private Sub StartProcess(ByVal CommChannel As String, ByVal status As Integer)
Dim fileName As String = ServerConfig.OpcToDbFilePath & CommChannel & "\" & GetProcName(CommChannel)
fileName &= ".exe"
CopyFiles(fileName, CommChannel)
System.Threading.Thread.Sleep(200)
Try
If System.IO.File.Exists(fileName) Then
Dim opcProcess As New Process
Try
opcProcess.StartInfo.UseShellExecute = True
opcProcess.StartInfo.FileName = fileName
opcProcess.StartInfo.CreateNoWindow = True
opcProcess.Start()
System.Threading.Thread.Sleep(500)
Catch ex As Exception
...
End Try
Else
...
Catch ex As Exception
...
End Try
End Try
End Sub
直到第28个程序启动后才能正常工作。之后,我试图开始的表单程序意外关闭。
我是否达到某种限制,可以打开多少个实例?服务和Forms程序在Windows Server Datacenter 2012上运行。
答案 0 :(得分:1)
看起来你在try catch循环之外发生了异常。这可能是由初始化程序等引起的。要捕获winforms应用程序中的所有异常,我会执行以下操作:
使用新的启动代码添加模块。注意:不要在此模块中初始化成员变量,直到您进入Main的try catch循环
Public Sub Main()
Try
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf HandleFatalException
AddHandler Application.ThreadException, AddressOf HandleFatalException
Using frm As New MyForm
Application.Run(frm)
End Using
Catch ex As Exception
HandleFatalException(ex)
End Try
End Sub
Private Sub HandleFatalException(ex As Exception)
Log.Fatal("Unhandled Exception", ex)
MessageBox.Show(ex.Message & vbCrLf & "See log file from more details", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Application.Exit()
End Sub
Private Sub HandleFatalException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
HandleFatalException(CType(e.ExceptionObject, Exception))
End Sub
Private Sub HandleFatalException(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
HandleFatalException(e.Exception)
End Sub
答案 1 :(得分:0)
对于多少句柄和GDI对象的限制如下所述:
Pushing the Limits of Windows: Handles
Pushing the Limits of Windows: USER and GDI Objects – Part 1
Pushing the Limits of Windows: USER and GDI Objects – Part 2
这些可能会限制您可以打开多少个窗口/进程。句柄因架构而异,GDI的默认最大计数为10,000。
桌面堆也是我的问题所在: Desktop Heap Overview
我在" HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Control \ Session Manager \ SubSystems \ Windows"中修改了第三个数值。到3072年(以前是768年)和中提琴!魔法。