title: Application Controller and Application.Run() - Stop Program When UI Ends
body: <p>I have an application controller with navigation workflow like so:</p>
  
  <pre><code>namespace Ordering.UI.Workflow
  {
      public static class ApplicationController
      {
          private static INavigationWorkflow instance;
          private static string navigationArgument;
  
          public static void Register(INavigationWorkflow service)
          {
              if (service == null)
                  throw new ArgumentNullException();
              instance = service;
          }
  
          public static void NavigateTo(string view)
          {
              if (instance == null)
                  throw new InvalidOperationException();
              instance.NavigateTo(view, Argument);
          }
  
          public static void NavigateTo(string view, string argument)
          {
              if (instance == null)
                  throw new InvalidOperationException();
              navigationArgument = argument;
              NavigateTo(view);
          }
  
          public static string Argument
          {
              get
              {
                  return navigationArgument;
              }
          }
  
      }
  }
  </code></pre>
  
  <p>NavigationWorkflow Class:</p>
  
  <pre><code>namespace Ordering.UI.Workflow
  {
  
  
  public interface INavigationWorkflow
  {
      void NavigateTo(string uri, string argument);
  }
  
  public class NavigationWorkflow : INavigationWorkflow
      {
  
      Form _mainForm;
      ProductScreen productScreen;
  
      public NavigationWorkflow() { }
  
      public NavigationWorkflow(Form mainForm)
      {
          _mainForm = mainForm;
      }
  
      public void NavigateTo(string view, string argument)
      {
          switch (view)
          {
              case "products":
                  if (productScreen != null &amp;&amp; !productScreen.IsDisposed)
                  {
                      productScreen.Close();
                      productScreen = null;
                  }
                  if (productScreen == null &amp;&amp; productScreen.IsDisposed)
                  {
                      productScreen = new ProductScreen();
                  }
                  productScreen.Show();
                  break;
          }
      }
  }
   }
  </code></pre>
  
  <p>and in my Program.cs, I want to do this:</p>
  
  <pre><code>OrderScreen orderScreen = new OrderScreen();
  orderScreen.Show();
  
  NavigationWorkflow workflow = new NavigationWorkflow(orderScreen);
  ApplicationController.Register(workflow);
  
  Application.Run();
  </code></pre>
  
  <p>But whenever my main form (<code>OrderScreen</code>) closes, the main application continues to run. How can I register my closing event with the <code>Application.Run()</code>? Do I have to create my own <code>ApplicationContext</code>? Is there some way to automatically do this?</p>