Nagendra Babu
Nagendra Babu

Reputation: 69

Get notified if started process exited

I am Running a WPF application (process A). From this on a button click I need to start another process (process B), which is a WinForms application.

Now my question is if I close process B's window by clicking the (X)-button, which closes process B abruptly then how to notify this event info to process A?

Upvotes: 0

Views: 230

Answers (2)

Clemens
Clemens

Reputation: 128060

The Process class has an Exited event for exactly this purpose. You also have to set EnableRaisingEvents to true to use it:

private void ButtonClick(object sender, RoutedEventArgs e)
{
    var process = new Process();
    process.StartInfo.FileName = "cmd.exe";
    process.EnableRaisingEvents = true;
    process.Exited += ProcessExited;
    process.Start();

    startButton.IsEnabled = false;
}

private void ProcessExited(object sender, EventArgs e)
{
    Dispatcher.BeginInvoke(new Action(() => startButton.IsEnabled = true));
}

Upvotes: 1

Shell
Shell

Reputation: 6849

if you are executing your external application using Process.Start then you can use WaitForExit method that will hold your cursor until the application is not exited. Take a look at this example to understand more about WaitForExit method on MSDN.

Upvotes: 1

Related Questions