Exit code from Windows Forms app

后端 未结 3 1160
离开以前
离开以前 2021-02-12 17:46

How do i return a non-zero exit code from a Windows Forms application.

Application.Exit() is the preferred way to exit the application, but there is no exit code argumen

3条回答
  •  不知归路
    2021-02-12 18:22

    Application.Exit just force the call to Application.Run (That is typically in program.cs) to finish. so you could have :

    Application.Run(new MyForm());
    Environment.Exit(0);
    

    and still inside your application call Application.Exit to close it.

    Small sample

    class Program
    {
        static int exitCode = 0;
    
        public static void ExitApplication(int exitCode)
        {
            Program.exitCode = exitCode;
            Application.Exit();
        }
    
        public int Main()
        {
            Application.Run(new MainForm());
            return exitCode;
        }
    }
    
    class MainForm : Form
    {
        public MainForm()
        {
            Program.ExitApplication(42);
        } 
    }
    

提交回复
热议问题