使用 Application.Exit 退出应用程序而不显示 GUI

发布于 2024-11-03 13:13:50 字数 515 浏览 1 评论 0原文

概括: 当应用程序未使用指定的用户帐户运行时,应用程序将退出,但仍会创建 GUI。我希望它完全退出

详细信息: 我本以为下面的代码可以工作。它检查用户名的特定属性,如果没有,应用程序将关闭。简单的。问题在于,即使用户名不包含该属性,应用程序仍然会创建 GUI。有什么想法吗?

这段代码在我的 main 方法中。它是一个windows窗体。

代码:

public ProgramMain()
{
    String runningUser = Environment.UserName;
    if (!runningUser.Contains("asdf"))
    {
        MessageBox.Show("You must run this application with your asdf account.");
        Application.Exit();
    }
    else
    {
        InitializeComponent();
    }
}

Summary:
When the application is not run with a specified user account the application exits but still creates a GUI. I want it to completely exit

Details:
I would have thought that the code I have below would work. It checks for a specific attribute of the username, if it doesn't have it, the app closes. Simple. The problem is that the application still creates a GUI even if the user name doesn't contain that attribute. Any ideas?

This code is in my main method. It is a windows form.

Code:

public ProgramMain()
{
    String runningUser = Environment.UserName;
    if (!runningUser.Contains("asdf"))
    {
        MessageBox.Show("You must run this application with your asdf account.");
        Application.Exit();
    }
    else
    {
        InitializeComponent();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温柔戏命师 2024-11-10 13:13:50

在 Main 中的 Program.cs 文件中,您将看到 Application.Run() 如果您在执行此行代码之前进行检查。然后如果用户不正确,则跳过该行。

Application.Run() 是启动应用程序运行循环的行。

如果 Run Loop 从未被命中并且 main 退出。应用程序将退出。

In the Program.cs file in Main you will see Application.Run() If you do the check before this line of code is hit. Then if the user is not correct, just skip the line.

Application.Run() is line to start the application run loop.

If the Run Loop is never hit and main is exited. the application will exit.

风苍溪 2024-11-10 13:13:50

您正在表单中编码,而不是在program.cs 中的Main() 事件中编码。如果在实际实例化表单之前退出,您将不会获得 GUI。

You are coding in the form and not in the Main() event in program.cs. If you exit before the form is actually instantiated, you will not get a GUI.

一身软味 2024-11-10 13:13:50

你太远了。 InitializeComponent 看起来您已经位于 Windows 窗体中。那么你已经创建了一个窗口。在创建表单之前将检查放入 main 方法中,然后不会显示任何内容。
您可以中断调试器并检查调用堆栈。搜索代码来自您的项目的第一个方法。用这个方法进行检查,你应该没问题。

你的,
阿洛伊斯·克劳斯

You are too far. InitializeComponent looks like you are already inside a Windows Form. Then you have already created a Window. Put your check in the main method before the form is created then nothing is displayed.
You can break in the debugger and check the call stack. Search for the first method where the code is from your project. Put in this method the check and you should be fine.

Yours,
Alois Kraus

风柔一江水 2024-11-10 13:13:50

看起来您是在控件的构造函数调用中执行此操作。尝试在 Program 类的 Main 方法中执行此检查。

It seems like you are doing it in the constructor call of your control.Try doing this checking in the Main method of Program class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文