private void StartAppWatcher(string elementName, FindWindowMethod method)
 {
     windowElement = GetAppElement(elementName, method);
     // (...)
     // You may want to to something if the watched application is already running when you start your app
     Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement,
                                          TreeScope.Subtree, (elm, e) => {
         AutomationElement element = elm as AutomationElement;
         try
         {
             if (element == null || element.Current.ProcessId == currentProcessId)
             {
                 return;
             }
             if (windowElement == null)
             {
                 windowElement = GetAppElement(elementName, method);
             }
             if (windowElement == null || windowElement.ProcessId != element.Current.ProcessId)
             {
                 return;
             }
             // If the Window is a MessageBox generated by the watched app, attach the handler
             if (element.Current.ClassName == "#32770")
             {
                 buttonElement    = element;
                 var msgBoxButton = element.FindFirst(TreeScope.Descendants,
                                                      new PropertyCondition(AutomationElement.NameProperty, "OK"));
                 if (msgBoxButton != null && msgBoxButton.GetSupportedPatterns().Any(p => p.Equals(InvokePattern.Pattern)))
                 {
                     Automation.AddAutomationEventHandler(
                         InvokePattern.InvokedEvent, msgBoxButton, TreeScope.Element,
                         DialogButtonHandler = new AutomationEventHandler(MessageBoxButtonHandler));
                 }
             }
         }
         catch (ElementNotAvailableException) {
             // Ignore: this exception may be raised if you show a modal dialog,
             // in your own app, that blocks the execution. When the dialog is closed,
             // AutomationElement element is no longer available
         }
     });
     Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, AutomationElement.RootElement,
                                          TreeScope.Subtree, (elm, e) => {
         AutomationElement element = elm as AutomationElement;
         if (element == null || element.Current.ProcessId == currentProcessId || windowElement == null)
         {
             return;
         }
         if (windowElement.ProcessId == element.Current.ProcessId)
         {
             if (windowElement.MainWindowTitle == element.Current.Name)
             {
                 windowElement = null;
             }
         }
     });
 }
    private ElementWindow GetAppElement(string elementName, FindWindowMethod method)
    {
        Process proc = null;

        try {
            switch (method)
            {
            case FindWindowMethod.ProcessName:
                proc = Process.GetProcessesByName(elementName).FirstOrDefault();
                break;

            case FindWindowMethod.Caption:
                proc = Process.GetProcesses().FirstOrDefault(p => p.MainWindowTitle == elementName);
                break;
            }
            return(CreateElementWindow(proc));
        }
        finally {
            proc?.Dispose();
        }
    }