Hi!
I wonder if you can reproduce the problem I have encountered with the UiaComWrapper.
I have made a simple WPF test application with a single button that will show a message box when the button is clicked. After the message box has been shown, any attempt to remove automation event handlers will hang forever. It does not matter whether the message box has been closed or not; it will hang anyway.
Everything works fine if the message box has not been shown.
I run the test application with the following code:
internal class Program { private static readonly ManualResetEventSlim WaitHandle = new ManualResetEventSlim(); private static void Main(string[] args) { var targetApp = @"..\..\..\MessageBoxApplication\bin\Debug\MessageBoxApplication.exe"; Debug.Assert(Thread.CurrentThread.GetApartmentState() == ApartmentState.MTA); var process = Process.Start(targetApp); IntPtr handle = IntPtr.Zero; while (handle == IntPtr.Zero) { handle = process.MainWindowHandle; Thread.Sleep(100); } var element = AutomationElement.FromHandle(handle); var request = new CacheRequest(); request.Add(AutomationElement.AutomationIdProperty); using (request.Activate()) { Automation.AddAutomationEventHandler(InvokePattern.InvokedEvent, element, TreeScope.Subtree, HandleInvokedEvent); } // Wait until first event is received. WaitHandle.Wait(); Console.WriteLine("Sleeping for five seconds so that the message box can be closed..."); Thread.Sleep(5000); // RemoveAutomationEventHandler() will not return if the user interface has shown a message box in event handler. // It will be waiting for Factory.RemoveAutomationEventHandler(eventId.Id, element.NativeElement, listener); to return. Automation.RemoveAutomationEventHandler(InvokePattern.InvokedEvent, element, HandleInvokedEvent);
Console.WriteLine("Automation event handler removed."); process.CloseMainWindow(); } private static void HandleInvokedEvent(object sender, AutomationEventArgs e) { var element = (AutomationElement)sender; Console.WriteLine("InvokedEvent raised by {0}.", element.Cached.AutomationId); WaitHandle.Set(); } }
Test application's xaml:
<Window x:Class="MessageBoxApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"><Grid><Button x:Name="TestButton" Click="TestButton_Click">Test Button</Button></Grid></Window>
Test application's code-behind:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void TestButton_Click(object sender, RoutedEventArgs e) { MessageBox.Show("Hello!"); } }
Do you have any ideas what might be causing this?