Selim Boz
Selim Boz

Reputation: 29

How do I know when the Process has started?

I want to open an application inside a Panel in a Form.
The name of the application is Stellarium. When the application first opens, it presents a splash screen, as in picture 1:

enter image description here

After a while, the main program opens as in picture 2:

enter image description here

The problem is this: when I run the code below, the application opens and the splash screen in picture 1 is placed inside the Panel, but the main application (picture 2) opens outside the Panel.
What should I do to open the application inside the Panel?

My code:

private void Notepad_Button_Click(object sender, EventArgs e)
{
    var psi = new ProcessStartInfo(@"C:\Program Files\Stellarium\stellarium.exe") { 
        WindowStyle = ProcessWindowStyle.Minimized 
    };

    using (var p = Process.Start(psi))
    {
        p.WaitForInputIdle();
       
        var handle = p.MainWindowHandle;
        
        if (handle != IntPtr.Zero)
        {
            SetParent(handle, panel1.Handle);
            MoveWindow(handle, 0, 0, panel1.Width, panel1.Height, true);
        }
    }
}

When I add a timer like the code below, the application opens inside the Panel.
But using a Timer is not the right solution, in my opinion.
Thanks you for your support.

private void Notepad_Button_Click(object sender, EventArgs e)
{
    var psi = new ProcessStartInfo(@"C:\Program Files\Stellarium\stellarium.exe") { WindowStyle = ProcessWindowStyle.Minimized };
    using (var p = Process.Start(psi))
    {
        p.WaitForInputIdle();
       
        var handle = p.MainWindowHandle;
        System.Threading.Thread.Sleep(20000);

        if (handle != IntPtr.Zero)
        {
            SetParent(handle, panel1.Handle);
            MoveWindow(handle, 0, 0, panel1.Width, panel1.Height, true);
        }
    }
}

Upvotes: 1

Views: 94

Answers (0)

Related Questions