Vb.net WPF 调度程序委托内存泄漏帮助

发布于 2024-10-05 18:38:38 字数 927 浏览 1 评论 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
            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?

谢谢!

I am using the following code to test a problem I am trying to solve.

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

the application leaks memory slowly but noticeably.

how do I clean up (or dispose of) each delegate I am creating?
since there is no d.dispose?

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最单纯的乌龟 2024-10-12 18:38:38

我发现,如果您在全局范围内声明新委托并根据需要调用,它可以解决此问题:

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

I figured out that if you declare the new delegate in the global scope and invoke as needed it fixes this problem:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文