Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API for default wait time when getting MainWindow #51

Open
JohanLarsson opened this issue Feb 28, 2018 · 7 comments
Open

API for default wait time when getting MainWindow #51

JohanLarsson opened this issue Feb 28, 2018 · 7 comments

Comments

@JohanLarsson
Copy link
Member

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
Copy link

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
Copy link
Member Author

Sounds like a bug, have you seen anything dumb in the code?

/// <summary>
/// Waits until the main handle is set.
/// </summary>
/// <param name="process">The process.</param>
/// <param name="waitTimeout">An optional timeout. If null is passed, the timeout is infinite.</param>
public static void WaitForMainWindow(Process process, TimeSpan? waitTimeout = null)
{
var start = DateTime.Now;
do
{
if (process.HasExited)
{
throw new InvalidOperationException("Process has exited.");
}
process.Refresh();
if (process.MainWindowHandle != IntPtr.Zero)
{
return;
}
Wait.For(Retry.PollInterval);
}
while (!Retry.IsTimeouted(start, waitTimeout ?? TimeSpan.MaxValue));
throw new TimeoutException("Did not find Process.MainWindowHandle, if startup is slow try with a longer timeout.");
}

@davishoang96
Copy link

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

@JohanLarsson
Copy link
Member Author

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

@davishoang96
Copy link

Yes it did throw timeout exception

@JohanLarsson
Copy link
Member Author

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

@davishoang96
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants