下午好, 我正在写一个在我们公司使用的应用程序。在其中一个主要表单上,有3个面板,每个面板都添加和删除了自定义用户控件。但是,每次计时器运行并添加/删除控件时,应用程序的内存使用量都会上升,并且永远不会再次下降。如果我添加一些控件并将它们留在那里,内存使用量最终将升至1.4GB,应用程序将崩溃。这是在计时器中运行的代码。我显然做了一些非常错误的事情,并希望得到任何可以提供的帮助。
Private Sub tmrCoreCalls_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrCoreCalls.Tick
Dim newCoreCalls As New ArrayList
tmrCoreCalls.Enabled = False
Dim a As New SqlDataProvider.SqlDatabase("Data Source=" & getDatabaseServer() & ";Initial Catalog=Application;User ID=XXXX;Password=XXXX")
Dim myreader As IDataReader = a.ExecuteReader("vault.dbo.CallControl_getcorequeuedcalls", CommandType.StoredProcedure)
Dim tmp, tmp2 As objCoreCall
While myreader.Read
tmp = New objCoreCall
tmp.CallID = myreader("callid").ToString
tmp.Ani = myreader("ani").ToString
tmp.Line = myreader("line").ToString
tmp.Queue = myreader("queue").ToString
tmp.Wait = myreader("wait").ToString
tmp.Channel = myreader("channel").ToString
tmp.DNIS = myreader("dnis").ToString
tmp.CallType = "CORE"
If InStr(UCase(tmp.Queue), "PARK") Then
tmp.BackColor = Drawing.Color.Coral
End If
newCoreCalls.Add(tmp)
tmp = Nothing
End While
If qcalls.Controls.Count = 0 Then
For Each tmp In newCoreCalls
qcalls.Controls.Add(tmp)
If InStr(UCase(tmp.Queue), "PARK") Then
Else
System.Diagnostics.Debug.Print("Insert from 0 records: " & tmp.Queue)
My.Computer.Audio.Play(Windows.Forms.Application.StartupPath & "\priCall.wav", AudioPlayMode.Background)
End If
Next
myreader.Close()
tmrCoreCalls.Enabled = True
newCoreCalls = Nothing
a = Nothing
myreader = Nothing
tmp2 = Nothing
tmp = Nothing
Exit Sub
End If
Dim i As Integer
'Remove all dead ones
Dim isfound As Boolean = False
For Each tmp3 As objCoreCall In qcalls.Controls
For Each tmp In newCoreCalls
If tmp.CallID = tmp3.CallID Then
isfound = True
tmp3.Wait = tmp.Wait
End If
Next
If isfound = False Then
'qcalls.Controls(i).Dispose()
qcalls.Controls.Remove(tmp3)
tmp3.Dispose()
tmp3 = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
Else
isfound = False
End If
Next
' Add new calls
isfound = False
For Each tmp In newCoreCalls
For Each tmp3 As objCoreCall In qcalls.Controls
If tmp.CallID = tmp3.CallID Then
isfound = True
End If
Next
If isfound = False Then
qcalls.Controls.Add(tmp)
If InStr(UCase(tmp.Queue), "PARK") Then
Else
System.Diagnostics.Debug.Print("Insert from Added records: " & tmp.Queue)
My.Computer.Audio.Play(Windows.Forms.Application.StartupPath & "\priCall.wav", AudioPlayMode.Background)
End If
End If
isfound = False
Next
For Each tmp2 In qcalls.Controls
If InStr(UCase(tmp2.Queue), "PARK") Then
tmp2.BackColor = Drawing.Color.Coral
End If
Next
myreader.Close()
newCoreCalls = Nothing
a = Nothing
myreader = Nothing
tmp2 = Nothing
tmp = Nothing
Label1.Text = "Queue Core Calls (" & qcalls.Controls.Count & ")"
tmrCoreCalls.Enabled = True
End Sub
我将不胜感激任何帮助和见解。如果有任何控件可见,则运行时,内存使用量会不断增加。添加多个控件会以指数方式增加内存使用量。
非常感谢。