<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;

namespace Signum.Windows.UIAutomation
{
    public class WindowProxy : IDisposable
    {
        public AutomationElement Element { get; private set; }

        private WindowPattern wp;

        public WindowProxy(AutomationElement element)
        {
            this.Element = element;
            wp = element.Pattern<WindowPattern>();
        }

        public virtual void Dispose()
        {
            Close();
        }

        public void Wait(int milliseconds)
        {
            wp.WaitForInputIdle(milliseconds);
        }

        public virtual bool Close()
        {
            try
            {
                wp.Close();

                return true;
            }
            catch (ElementNotAvailableException ena)
            {
                if (ena.Message.Contains("The target element corresponds to UI that is no longer available (for example, the parent window has closed)."))
                    return false;

                throw ena;
            }
        }

        public MessageBoxProxy TryGetCurrentMessageBox()
        {
            var win = Element.TryChild(a => a.Current.ControlType == ControlType.Window && a.Current.ClassName == "#32770");

            if (win != null)
                return new MessageBoxProxy(win);

            return null;
        }

        public MessageBoxProxy WaitCurrentMessageBox()
        {
            var win = Element.WaitChild(a => a.Current.ControlType == ControlType.Window && a.Current.ClassName == "#32770");

            return new MessageBoxProxy(win);
        }
    }


    public class MessageBoxProxy : WindowProxy
    {
        public MessageBoxProxy(AutomationElement element): base(element)
        {
        }

        public AutomationElement OkButton
        {
            get { return Element.TryChildById("1") ?? Element.ChildById("2"); }
        }

        public AutomationElement CancelButton
        {
            get { return Element.ChildById("2"); }//Warning!!
        }

        public AutomationElement YesButton
        {
            get { return Element.ChildById("6"); }
        }

        public AutomationElement NoButton
        {
            get { return Element.ChildById("7"); }
        }
    }
}
<commit_msg>Avoid exceptions after closing WindowProxy if possible<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;

namespace Signum.Windows.UIAutomation
{
    public class WindowProxy : IDisposable
    {
        public AutomationElement Element { get; private set; }

        private WindowPattern wp;

        public WindowProxy(AutomationElement element)
        {
            this.Element = element;
            wp = element.Pattern<WindowPattern>();
        }

        public virtual void Dispose()
        {
            Close();
        }

        public void Wait(int milliseconds)
        {
            wp.WaitForInputIdle(milliseconds);
        }

        public virtual bool Close()
        {
            try
            {
                if (Element.Current.IsOffscreen)
                    return false;

                wp.Close();

                return true;
            }
            catch (ElementNotAvailableException ena)
            {
                if (ena.Message.Contains("The target element corresponds to UI that is no longer available (for example, the parent window has closed)."))
                    return false;

                throw ena;
            }
        }

        public MessageBoxProxy TryGetCurrentMessageBox()
        {
            var win = Element.TryChild(a => a.Current.ControlType == ControlType.Window && a.Current.ClassName == "#32770");

            if (win != null)
                return new MessageBoxProxy(win);

            return null;
        }

        public MessageBoxProxy WaitCurrentMessageBox()
        {
            var win = Element.WaitChild(a => a.Current.ControlType == ControlType.Window && a.Current.ClassName == "#32770");

            return new MessageBoxProxy(win);
        }
    }


    public class MessageBoxProxy : WindowProxy
    {
        public MessageBoxProxy(AutomationElement element): base(element)
        {
        }

        public AutomationElement OkButton
        {
            get { return Element.TryChildById("1") ?? Element.ChildById("2"); }
        }

        public AutomationElement CancelButton
        {
            get { return Element.ChildById("2"); }//Warning!!
        }

        public AutomationElement YesButton
        {
            get { return Element.ChildById("6"); }
        }

        public AutomationElement NoButton
        {
            get { return Element.ChildById("7"); }
        }
    }
}

{'fraction_non_alphanumeric': 0.08310194367314558, 'fraction_numerical': 0.005950019833399445, 'mean_word_length': 1.515211970074813, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '415802', 'n_tokens_mistral': 1282, 'n_tokens_neox': 1174, 'n_words': 292}