Forms应用程序退出代码

Forms应用程序退出代码

本文介绍了从Windows Forms应用程序退出代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Windows Forms应用程序返回非零退出代码.

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

Application.Exit()是退出应用程序的首选方法,但是没有退出代码参数.

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

我了解Environment.Exit(),但这不是关闭应用程序循环的好方法....

I know about Environment.Exit(), but that is not a nice way to close the application loop....

推荐答案

Application.Exit 只需强制调用 Application.Run (通常在 program.cs 中)完成.这样就可以了:

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

并仍在您的应用程序内部,调用 Application.Exit 将其关闭.

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

小样本

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

这篇关于从Windows Forms应用程序退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:22