Icemanind
Icemanind

Reputation: 48686

Launching an application, then restoring after closing application

I have an application that launches Notepad. What I want to do is minimize my application, then launch Notepad.exe. After the user closes notepad, I'd like my application to automatically maximize or restore my application window. How can I do this in C#? I'm looking into hooking into the notepad.exe process and trying to detect window close event on notepad. I seem to be way over thinking this though. Is there a simple way to do this?

Upvotes: 1

Views: 272

Answers (1)

Hans Passant
Hans Passant

Reputation: 941397

Just use the Exited event to restore your window. Like this:

    private void button1_Click(object sender, EventArgs e) {
        var prc = new System.Diagnostics.Process();
        prc.StartInfo.FileName = "notepad.exe";
        prc.EnableRaisingEvents = true;
        prc.SynchronizingObject = this;
        prc.Exited += delegate { 
            this.WindowState = FormWindowState.Normal;
            prc.Dispose();
        };
        prc.Start();
        this.WindowState = FormWindowState.Minimized;
    }

Upvotes: 1

Related Questions