---
title: "How To Know When An External Application Exits"
url: "https://osherove.com/blog/2003/6/19/how-to-know-when-an-external-application-exits.md"
canonical: "https://osherove.com/blog/2003/6/19/how-to-know-when-an-external-application-exits.html"
published: "2003-06-19T11:36:00"
updated: "2003-06-19T11:36:00"
tags: [".Net Original"]
author: "Roy Osherove"
description: "I was asked how one can launch an external application and know when it is finished. Her'es the code (Console Application): using System . Diagnostics ; static void Main ( string [] args ) { Process p = new Process ();…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# How To Know When An External Application Exits

By Roy Osherove June 19, 2003 In [.Net Original](https://osherove.com/blog/category/net-original)

**Historical article** · Originally published June 19, 2003; technical details may be outdated.

I was asked how one can launch an external application and know when it is finished. Her'es the code (Console Application):

using System.Diagnostics;

static void Main(string\[\] args)

{

      Process p  = new Process();

      //Register for the event that notifies the process exit

      p.Exited+=new EventHandler(OnExit);

      p.StartInfo.FileName\= "Notepad.exe";

      //you must enable this property or you won't get the event

      p.EnableRaisingEvents\=true;

      p.Start();

      Console.WriteLine("Waiting...");

      Console.ReadLine();

}

private static void OnExit(object sender,EventArgs e)

{

      //here you get the event of a process close

      Console.WriteLine("The process was finished!");

}

You can also avoid using an event handler and simply make the code wait for the process(Synchronous invocation). Here are the two possibilities:

p.Start();

//wait indefinitely for the process to finish

p.WaitForExit();

//this line will be called only when the process finishes

Console.WriteLine("Waiting...");

Or you could wait for a maximum number of milliseconds(a sort of timeout):

p.Start();

//wait up to 5 seconds for the process to finish

p.WaitForExit(5000);

//this line will be called after 5 seconds

//or when the process finishes

Console.WriteLine("Waiting...");

## More in .Net Original

[All posts in .Net Original](https://osherove.com/blog/category/net-original)

- Jun 22, 2003

    ### [A Little Macro To Automate Creating Connection Strings](https://osherove.com/blog/2003/6/22/a-little-macro-to-automate-creating-connection-strings.html)

- Jun 21, 2003

    ### [Winforms Data Binding Lessons Learned](https://osherove.com/blog/2003/6/21/winforms-data-binding-lessons-learned-1.html)

- Jun 19, 2003

    ### [Winforms DataBinding Bliss](https://osherove.com/blog/2003/6/19/winforms-databinding-bliss.html)

- Jun 16, 2003

    ### [Funny Behaviour](https://osherove.com/blog/2003/6/16/funny-behaviour.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
