GuOrg / Gu.Wpf.UiAutomation

MIT License
96 stars 17 forks source link

API for default wait time when getting MainWindow #51

Open JohanLarsson opened 6 years ago

JohanLarsson commented 6 years ago

Now it is

public Window MainWindow => this.GetMainWindow(TimeSpan.FromSeconds(10));

Which is not very nice and the time is probably too short for real applications. Static property is an alternative but not very nice. Another alternative is attribute.

davishoang96 commented 6 years ago

I have this GetMainWindow function for the program. I did try TimeSpan.FromSeconds(5) but i'm still getting infinity waiting for the respond. Do you have any idea?

    public static void GetMainWindow()
    {
        try
        {
            Process AttachProcess = WindowInteraction.GetProcess(ProcessForm.targetproc);

            App = Application.Attach(AttachProcess.Id);

            MainWindow = App.GetMainWindow(TimeSpan.FromSeconds(5));

        }
        catch
        {

            throw new Exception("Cannot get MainWindow");
        }

    }
JohanLarsson commented 6 years ago

Sounds like a bug, have you seen anything dumb in the code? https://github.com/GuOrg/Gu.Wpf.UiAutomation/blob/2916d6685b3cd993fd1bebd69b64ef7568551119/Gu.Wpf.UiAutomation/Application.cs#L344-L369

davishoang96 commented 6 years ago

I did try App.WaitForMainWindow(TimeSpan.FromSeconds(1)); Although the time is still longer than 1 secs

JohanLarsson commented 6 years ago

Did it timeout when you did App.WaitForMainWindow(TimeSpan.FromSeconds(1));?

davishoang96 commented 6 years ago

Yes it did throw timeout exception

JohanLarsson commented 6 years ago

Do you have API ideas for this? Static property App.WaitForMainWindowTime is not awesome but perhaps not so bad.

davishoang96 commented 6 years ago

Yeah I fixed the issue by putting: Application.WaitForMainWindow(AttachProcess, TimeSpan.FromSeconds(1)); before the App = Application.Attach(AttachProcess.Id);

It worked perfectly fine. Thanks Johan.