views:

26

answers:

1

I have an application controller with navigation workflow like so:

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

    }
}

NavigationWorkflow Class:

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 && !productScreen.IsDisposed)
                {
                    productScreen.Close();
                    productScreen = null;
                }
                if (productScreen == null && productScreen.IsDisposed)
                {
                    productScreen = new ProductScreen();
                }
                productScreen.Show();
                break;
        }
    }
}
 }

and in my Program.cs, I want to do this:

OrderScreen orderScreen = new OrderScreen();
orderScreen.Show();

NavigationWorkflow workflow = new NavigationWorkflow(orderScreen);
ApplicationController.Register(workflow);

Application.Run();

But whenever my main form (OrderScreen) closes, the main application continues to run. How can I register my closing event with the Application.Run()? Do I have to create my own ApplicationContext? Is there some way to automatically do this?

+1  A: 

Application.Run() automatically creates an ApplicationContext. You can get a reference to it with the Application.ApplicationContext property. Call its ExitThread() method to force the message loop to terminate.

Creating your own and passing it to Run() is also possible, no real advantage that I can think of. Other than it serving as a base class for your controller. The Run() method call logically belongs in your controller as well.

Hans Passant
Yes. Or count form instances and ExitThread() when the last one is closed.
Hans Passant
Sorry deleted my last.. So you are saying call Application.Run(form) in Program.cs or ApplicationController? If I call Application.Run() from the ApplicationController Register method, would it block?
0A0D
Run(mainForm) makes it automatic without you having to write any code. Yes, Run() blocks.
Hans Passant