我有一个简单的WPF程序,只有一个没有事件处理逻辑的按钮.然后我使用UIAutomation框架连续多次单击该按钮.最后,我看一下WPF程序使用的内存,它似乎在不断发展壮大.
任何人都知道为什么会这样,我怎么能防止这种情况发生?
这是简单的WPF程序(后面的代码中没有任何内容):
<Window x:Class="SimpleApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Simple Application"
AutomationProperties.AutomationId="Simple Application"
Height="350" Width="525">
<Grid>
<Button AutomationProperties.AutomationId="button" Height="50" Width="100">Click Me</Button>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
这是UIAutomation测试程序:
class Program
{
static void Main(string[] args)
{
string appPath = @"..\..\..\SimpleApplication\bin\Debug\SimpleApplication.exe";
string winAutoId = "Simple Application";
string buttonAutoId = "button";
using (Process process = Process.Start(new ProcessStartInfo(appPath)))
{
Thread.Sleep(TimeSpan.FromSeconds(1));
AutomationElement winElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, winAutoId));
for (int i = 0; i < 1001; i++)
{
AutomationElement buttonElement = winElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, buttonAutoId));
InvokePattern invokePattern = (InvokePattern)buttonElement.GetCurrentPattern(InvokePattern.Pattern);
invokePattern.Invoke(); …Run Code Online (Sandbox Code Playgroud)