2

我有一个使用 .net UIAutomation 的应用程序,它最终会耗尽内存并崩溃,只是监视正在显示和关闭的窗口。在 VB 中显示这一点似乎比在 C# 中更容易,但无论哪种方式都一样。它似乎是底层代理对象中的泄漏/池。大多数内存没有显示为 .net 内存使用。

关于如何让它停止泄漏并仍然监视 StructureChangedEvents 的任何想法?

Imports System.Windows.Automation
Public Class Form1

Delegate Sub AddListCallback(ByVal Text As String)
Dim UIAeventHandler As StructureChangedEventHandler

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    BtnStartStop.Text = "Stop"
    Subscribe()
End Sub

Private Sub BtnStartStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStartStop.Click
    If "Start" = BtnStartStop.Text Then
        BtnStartStop.Text = "Stop"
        Subscribe()
    Else
        BtnStartStop.Text = "Start"
        Unsubscribe()
        lbEvents.Items.Clear()
        GC.GetTotalMemory(True)
    End If
End Sub

Public Sub Subscribe()
    UIAeventHandler = New StructureChangedEventHandler(AddressOf OnUIAutomationEvent)
    Automation.AddStructureChangedEventHandler(AutomationElement.RootElement, TreeScope.Descendants, UIAeventHandler)
End Sub 

Public Sub Unsubscribe()
    Automation.RemoveStructureChangedEventHandler(AutomationElement.RootElement, UIAeventHandler)
End Sub

''' <summary>
''' AutomationEventHandler delegate.
''' </summary>
''' <param name="src">Object that raised the event.</param>
''' <param name="e">Event arguments.</param>
Private Sub OnUIAutomationEvent(ByVal src As Object, ByVal e As StructureChangedEventArgs)
    ' Make sure the element still exists. Elements such as tooltips can disappear
    ' before the event is processed.  
    If e.StructureChangeType = StructureChangeType.ChildrenInvalidated Then
        Exit Sub
    End If
    Dim sourceElement As AutomationElement
    Try
        sourceElement = DirectCast(src, AutomationElement)
    Catch ex As ElementNotAvailableException
        Exit Sub
    End Try
    ' TODO Handle any other events that have been subscribed to.
    Console.WriteLine( "Element : """ & sourceElement.Current.LocalizedControlType & """ """ & sourceElement.Current.Name _
       & """ Struct Change Type : " & [Enum].GetName(GetType(StructureChangeType), e.StructureChangeType) _
       & " Runtime ID : " & getString(e.GetRuntimeId()))
End Sub 

Private Function getString(ByVal ints As Integer()) As String
    getString = ""
    For Each i As Integer In ints
        getString = getString & " " & i.ToString
    Next
End Function
End Class
4

2 回答 2

1

当你取消订阅时,我会做这样的事情:

Automation.RemoveAllEventHandlers();
UIAeventHandler = null;

只要您有一个 AutomationEventHandler 对象,UIAutomation 就会使一些线程保持活动状态。它对我来说几乎是一个黑匣子,但上面已经解决了我遇到的所有问题。

于 2010-06-29T21:53:33.430 回答
1

@jaws 是正确的(还有一些额外的事情需要考虑),但它在 C# 中。对于 VB,您需要执行以下操作:

Automation.RemoveStructureChangedEventHandler(AutomationElement.RootElement, UIAeventHandler)
UIAeventHandler = Nothing

因为您没有将 UIAeventHandler 设置为空,所以当您取消订阅时,您正在重新分配已经在内存中的事件处理程序。

想一下。请注意,当表单加载并使用按钮订阅/取消订阅时,您正在调用订阅。然后当表单加载时,事件处理程序被分配并且应该可以正常工作,直到您点击取消订阅/订阅(按该顺序)。当您没有将 UIAeventHandler 设置为 Nothing 时,您会导致内存泄漏,因为您随后在订阅时将 UIAeventHandler 设置为 NEW 实例。因此,您还可以按照@jaws 的建议执行以下操作:

Imports System.Windows.Automation
Public Class Form1
    Dim UIAeventHandler as StructureChangedEventHandler

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BtnStartStop.Text = "Stop"
        Subscribe()
    End Sub

    Public Sub Subscribe()
        UIAeventHandler = New StructureChangedEventHandler(AddressOf OnUIAutomationEvent)
        Automation.AddStructureChangedEventHandler(AutomationElement.RootElement, TreeScope.Descendants, UIAeventHandler)
    End Sub 

    Public Sub Unsubscribe()
        Automation.RemoveStructureChangedEventHandler(AutomationElement.RootElement, UIAeventHandler)
        UIAeventHandler = Nothing   'Here is the Important part!
    End Sub

End Class
于 2020-08-20T14:29:58.710 回答