1
votes

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?

1
Without further checking, often you can spawn a windows process, get the process id, and have a timer check that process ID is still running, once its not, pop back your app - BugFinder
@BugFinder is on track - that would probably do it for you. - Chris Barlow
Yeah that is a good idea. See I knew I was way over complicating this. Put your comment into an answer and I will mark it as solved. Thanks! - Icemanind

1 Answers

1
votes

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;
    }