Egor Okhterov
Egor Okhterov

Reputation: 558

Check visibility of the child process window

I have two .NET applications:

  1. parent-app.exe (WPF)
  2. child-app.exe (WinForms)

The first application starts the second one

parent-app.exechild-app.exe

by means of the class System.Diagnostics.Process.

When user clicks a button on the interface of the parent-app.exe, I start the process of child-app.exe immediately. Because child-app.exe is a .NET application, is takes some time before user could see the window (especially, on slow systems).

I want to show the user an intermediate (possibly dialog) window from parent-app.exe. This dialog window should say that user action is being processed and he should wait for the window of child-app.exe to show up.


Questions:

  • How can I check from parent-app.exe visibility state of the window of child-app.exe?

  • Here is the longer question. How would you implement this system of showing intermediate window by taking into account the restriction that both programs use .NET?

Upvotes: 1

Views: 270

Answers (1)

ArgusMagnus
ArgusMagnus

Reputation: 669

As suggested here, you can try calling the Process.WaitForInputIdle method to wait for the MainWindowHandle to be created or periodically calling Process.Refresh and check if MainWindowHandle returns a valid handle. Perhaps this is enough for you, otherwise you can get additional information with the handle. In WinForms you could probably do something like this:

Form window = (Form)Control.FromHandle(process.MainWindowHandle);

I suspect there are similar solutions for other frameworks.

Update

I wrote this small sample, it is untested and I don't know if it works reliably (it may deadlock), but it should hint at some things you can try:

namespace SomeNS
{
    using System.Diagnostics;
    using System.Windows.Forms;

    public static class SomeClass
    {
        static void SomeMethod()
        {
            Process process = Process.Start("child-app.exe");
            // YourDialog is your dialog implementation inheriting from System.Windows.Window
            YourDialog dlg = new YourDialog();
            dlg.Loaded += (sender, e) =>
            {
                process.WaitForInputIdle();
                Form window = (Form)Control.FromHandle(process.MainWindowHandle);
                // Do something with the child app's window
                dlg.Close();
            };
            dlg.ShowDialog();
        }
    }
}

Upvotes: 1

Related Questions