{"action":"created","issue":{"url":"https://api.github.com/repos/JakeGinnivan/UIAComWrapper/issues/6","labels_url":"https://api.github.com/repos/JakeGinnivan/UIAComWrapper/issues/6/labels{/name}","comments_url":"https://api.github.com/repos/JakeGinnivan/UIAComWrapper/issues/6/comments","events_url":"https://api.github.com/repos/JakeGinnivan/UIAComWrapper/issues/6/events","html_url":"https://github.com/JakeGinnivan/UIAComWrapper/issues/6","id":105707308,"number":6,"title":"Automation.AddAutomationPropertyChangedEventHandler calls freezes app","user":{"login":"vforvoid","id":14206129,"avatar_url":"https://avatars.githubusercontent.com/u/14206129?v=3","gravatar_id":"","url":"https://api.github.com/users/vforvoid","html_url":"https://github.com/vforvoid","followers_url":"https://api.github.com/users/vforvoid/followers","following_url":"https://api.github.com/users/vforvoid/following{/other_user}","gists_url":"https://api.github.com/users/vforvoid/gists{/gist_id}","starred_url":"https://api.github.com/users/vforvoid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vforvoid/subscriptions","organizations_url":"https://api.github.com/users/vforvoid/orgs","repos_url":"https://api.github.com/users/vforvoid/repos","events_url":"https://api.github.com/users/vforvoid/events{/privacy}","received_events_url":"https://api.github.com/users/vforvoid/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-09-10T00:07:59Z","updated_at":"2015-09-22T19:04:11Z","closed_at":null,"body":"It seems like calling this method makes my application freeze.\r\nIt is happening on Factory.AddPropertyChangedEventHandler call\r\nhttps://github.com/JakeGinnivan/UIAComWrapper/blob/master/UIAComWrapper/Automation.cs#L127\r\n\r\nAny ideas why is it happening and how I can fix it?\r\nThanks in advance."},"comment":{"url":"https://api.github.com/repos/JakeGinnivan/UIAComWrapper/issues/comments/142387239","html_url":"https://github.com/JakeGinnivan/UIAComWrapper/issues/6#issuecomment-142387239","issue_url":"https://api.github.com/repos/JakeGinnivan/UIAComWrapper/issues/6","id":142387239,"user":{"login":"vforvoid","id":14206129,"avatar_url":"https://avatars.githubusercontent.com/u/14206129?v=3","gravatar_id":"","url":"https://api.github.com/users/vforvoid","html_url":"https://github.com/vforvoid","followers_url":"https://api.github.com/users/vforvoid/followers","following_url":"https://api.github.com/users/vforvoid/following{/other_user}","gists_url":"https://api.github.com/users/vforvoid/gists{/gist_id}","starred_url":"https://api.github.com/users/vforvoid/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vforvoid/subscriptions","organizations_url":"https://api.github.com/users/vforvoid/orgs","repos_url":"https://api.github.com/users/vforvoid/repos","events_url":"https://api.github.com/users/vforvoid/events{/privacy}","received_events_url":"https://api.github.com/users/vforvoid/received_events","type":"User","site_admin":false},"created_at":"2015-09-22T19:04:11Z","updated_at":"2015-09-22T19:04:11Z","body":"Ok, sorry for making you wait. I think it is some threading probmels. Check out this modified snippet:\r\n\r\n    using System;\r\n    using System.Collections.Generic;\r\n    using System.Linq;\r\n    using System.Text;\r\n    using System.Threading.Tasks;\r\n    using System.Windows.Automation;\r\n    using System.Windows;\r\n\r\n    namespace UIAComWrapperTest\r\n    {\r\n        class Program\r\n        {\r\n            static AutomationElement element;\r\n            static AutomationPropertyChangedEventHandler handler;\r\n            static AutomationFocusChangedEventHandler focusHandler;\r\n\r\n            static void Main(string[] args)\r\n            {\r\n                SubscribeToFocusChange();\r\n            }\r\n\r\n            public static void SubscribeToFocusChange()\r\n            {\r\n                focusHandler = new AutomationFocusChangedEventHandler(OnFocusChange);\r\n                Automation.AddAutomationFocusChangedEventHandler(focusHandler);\r\n\r\n                while (true)\r\n                {\r\n                    System.Threading.Thread.Yield();\r\n                }\r\n            }\r\n\r\n            private static void OnFocusChange(object src, AutomationFocusChangedEventArgs e)\r\n            {\r\n                try\r\n                {\r\n                    System.Diagnostics.Debug.WriteLine(\"Focus changed\");\r\n                    var newElement = src as AutomationElement;\r\n                    if (element != null && element == newElement)\r\n                        return;\r\n                    element = newElement;\r\n                    InitializePropertyChanged();\r\n                }\r\n                catch (ElementNotAvailableException ex) \r\n                {\r\n                    Console.WriteLine(ex);\r\n                }\r\n                catch (Exception ex)\r\n                {\r\n                    Console.WriteLine(ex);\r\n                }\r\n            }\r\n\r\n            public static void InitializePropertyChanged()\r\n            {\r\n                try\r\n                {\r\n                    //element = AutomationElement.FromPoint(new Point(1400, 800));\r\n                    //element = AutomationElement.FromPoint(new Point(1200, 681));\r\n                    System.Diagnostics.Debug.WriteLine(\"Class: \" + element.Current.LocalizedControlType);\r\n                    var patterns = element.GetSupportedPatterns();\r\n                    if (!patterns.Contains(ValuePattern.Pattern))\r\n                        return;\r\n                    var valuePattern = (ValuePattern)element.GetCurrentPattern(ValuePattern.Pattern);\r\n                    var value = valuePattern.Current.Value;\r\n                    handler = new AutomationPropertyChangedEventHandler(OnPropertyChanged);\r\n                    // Uncomment task starting work to bypass freezes (maybe i should use task queue on the main thread instead?). It will lead to a bug when we're able to subscribe to the same element's event multiple time\r\n                    // So we should keep track of elements and properties subscriptions if we don't want to run into it. \r\n                    // I think .NET vanilla wrapper had no such issue, but it's manageable.\r\n                    //Task.Factory.StartNew(() =>\r\n                    //{\r\n                        // This will freeze unless called from separate thread. This wouldn't freeze in vanilla wrapper.\r\n                        Automation.AddAutomationPropertyChangedEventHandler(\r\n                            element,\r\n                            TreeScope.Subtree,\r\n                            handler,\r\n                            ValuePattern.ValueProperty);\r\n                        System.Diagnostics.Debug.WriteLine(\"Subscribed to cahnges\");\r\n                    //}\r\n                    //);\r\n                }\r\n                catch (ElementNotAvailableException) { }\r\n                catch (Exception e)\r\n                {\r\n                    Console.WriteLine(e);\r\n                }\r\n            }\r\n\r\n            public static void OnPropertyChanged(object sender, AutomationPropertyChangedEventArgs e)\r\n            {\r\n                var valuePattern = (ValuePattern)(sender as AutomationElement).GetCurrentPattern(ValuePattern.Pattern);\r\n                var value = valuePattern.Current.Value;\r\n                System.Diagnostics.Debug.WriteLine(\"New value: \" + value);\r\n            }\r\n        }\r\n\r\n    }\r\n"}}