我正在使用以下代码来测试我想要解决的问题。
Class MainWindow
Dim bw As BackgroundWorker = New BackgroundWorker()
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
AddHandler bw.DoWork, AddressOf bw_DoWork
AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted
bw.RunWorkerAsync()
End Sub
Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
For i As Integer = 0 To 250000
Dim d As New test_delegate(AddressOf test)
Dispatcher.Invoke(d, " SUPER SUPER SUPERSPERSUPERSUPERvSUPERSUPERSUPERSUPERv LONG Test")
Next
End Sub
Private Delegate Sub test_delegate(ByVal txt As String)
Private Sub test(ByVal txt As String)
txtTest.Text = txt
End Sub
End Class
应用程序缓慢但缓慢地泄漏内存。
如何清理(或处置)我正在创建的每个代表? 既然没有d.dispose?
谢谢!
答案 0 :(得分:0)
我发现如果你在全局范围内声明新委托并根据需要调用它就可以解决这个问题:
Class MainWindow
Dim bw As BackgroundWorker = New BackgroundWorker()
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
AddHandler bw.DoWork, AddressOf bw_DoWork
AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted
bw.RunWorkerAsync()
End Sub
Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
For i As Integer = 0 To 250000
Dispatcher.Invoke(d, " SUPER SUPER SUPERSPERSUPERSUPERvSUPERSUPERSUPERSUPERv LONG Test")
Next
End Sub
Dim d As New test_delegate(AddressOf test)
Private Delegate Sub test_delegate(ByVal txt As String)
Private Sub test(ByVal txt As String)
txtTest.Text = txt
End Sub
End Class