开发者

Application Controller and Application.Run() - Stop Program When UI Ends

开发者 https://www.devze.com 2023-01-11 13:49 出处:网络
I have an application controller with navigation workflow like so: namespace Ordering.UI.Workflow { public static class ApplicationController

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 Navigat开发者_Python百科ionWorkflow() { }

    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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号