C# Application.Run() 和 Form.ShowDialog() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2314514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:20:15  来源:igfitidea点击:

What's the difference between Application.Run() and Form.ShowDialog()?

c#winforms

提问by Vilx-

In my application I want to show a login form first and then the main form if the login has been successful. Currently I'm doing it something like this:

在我的应用程序中,如果登录成功,我想先显示登录表单,然后显示主表单。目前我正在做这样的事情:

var A = new LoginForm();
if ( A.ShowDialog() == DialogResult.OK )
    Application.Run(new MainForm());

But then I started wondering - what's the point of the Application.Run()? Why not just do (new MainForm()).ShowDialog()as well? What's the difference? And what would be the correct way to achieve what I want?

但后来我开始想知道 - 有什么意义Application.Run()?为什么不这样做(new MainForm()).ShowDialog()呢?有什么不同?什么是实现我想要的正确方法?

采纳答案by adrianbanks

Application.Run(Form)starts a message loop on the current thread and displays the specified form. The message loop enables the form to receive Windows messages (eg, key presses, mouse clicks, paint invalidations) to allow it to appear responsive and have interaction with the user. When you call ShowDialog()on a Forminstance, it actually does a similar thing and creates a modal message loop for the form on which ShowDialoghas been called.

Application.Run(Form)在当前线程上启动消息循环并显示指定的表单。消息循环使表单能够接收 Windows 消息(例如,按键、鼠标点击、绘画失效)以允许它显示响应并与用户进行交互。当您调用ShowDialog()一个Form实例时,它实际上会做类似的事情,并为ShowDialog被调用的表单创建一个模式消息循环。

There is not much difference between the two calls. Application.Rundoes add some extra event handling enabling you to do some tidying up of resources when the main form is closed (see Application.ThreadExit).

这两个调用之间没有太大区别。Application.Run确实添加了一些额外的事件处理,使您能够在主窗体关闭时对资源进行一些整理(请参阅Application.ThreadExit)。

The recommended way to start WinForms applications is using Application.Run, but I suspect this is more of a convention than a rule. The biggest reason to use Application.Runis if you want to open multiple non-modal forms. You can do this using:

启动 WinForms 应用程序的推荐方法是使用Application.Run,但我怀疑这更像是一种约定而不是规则。使用的最大原因Application.Run是如果您想打开多个非模态表单。您可以使用以下方法执行此操作:

new Form().Show();
new Form().Show();
Application.Run();

You could not achieve this using the ShowDialog()method as one of the forms would have to be modal.

您无法使用该ShowDialog()方法实现这一点,因为其中一种形式必须是模态的。



As for your question of how to show a login form and then the main form if the login is successful, I think what you have is fine:

至于你的问题,如果登录成功,如何显示登录表单,然后显示主表单,我认为你所拥有的很好:

if (new LoginForm().ShowDialog() == DialogResult.OK)
{
    Application.Run(new MainForm());
}

The alternative is to do the plumbing yourself and open an instance of MainFormin the closing event of the LoginFormif the login was successful.

另一种方法是自己做管道并在登录成功MainForm的关闭事件中打开一个实例LoginForm

回答by Asad

Application.Run()is for the start of application while MainFormis part of the application and MainForm()).ShowDialog()used to display it only.

Application.Run()用于启动应用程序,而MainForm是应用程序的一部分,MainForm()).ShowDialog()仅用于显示它。

Application.Run()is the entry pointfor your Application. same as Main()method is for some class or ApplicationStart()for a WebApplication

Application.Run()entry point您的应用程序。与Main()用于某个类或ApplicationStart()WebApplication 的方法相同

Application.Run()has different overloads, one of which is without parameters. That Method starts application without an initial form.

Application.Run()有不同的重载,其中之一是没有参数的。该方法在没有初始形式的情况下启动应用程序。

回答by BFree

From MSDN:

来自 MSDN:

This method adds an event handler to the mainForm parameter for the Closed event. The event handler calls ExitThread to clean up the application.

此方法将事件处理程序添加到 Closed 事件的 mainForm 参数。事件处理程序调用 ExitThread 来清理应用程序。

http://msdn.microsoft.com/en-us/library/ms157902.aspx

http://msdn.microsoft.com/en-us/library/ms157902.aspx

回答by Chad Stewart

One key difference is that ShowDialog is usually a modal Dialog. If you wanted to create a user-friendly toolset, you would not want it to be comprised of modal dialog boxes.

一个关键的区别是 ShowDialog 通常是一个模态对话框。如果您想创建一个用户友好的工具集,您不会希望它由模态对话框组成。

Also, Application.Run() accepts more than just a form. It has a few overloads.

此外,Application.Run() 不仅仅接受表单。它有一些重载。

As for your application, I do not think it matters much. Application.Run makes sense to me because it denotes the start of your actual Application.

至于你的申请,我认为没有多大关系。Application.Run 对我来说很有意义,因为它表示您的实际应用程序的开始。

回答by John

From my testing, I noticed this main difference:

从我的测试中,我注意到了这个主要区别:

When Application.Run is used, the form's Close button (red X) returns DialogResult.None; however, when ShowDialog is used, the Close button produces DialogResult.Cancel.

使用 Application.Run 时,窗体的关闭按钮(红色 X)返回 DialogResult.None;但是,当使用 ShowDialog 时,Close 按钮​​会生成 DialogResult.Cancel。

Does this matter to you? In my code, I was testing for DialogResult.Cancel to determine the exit code of my application. That was broken when the red X was used to close the form. I now test for DialogResult.OK to indicate a successful exit.

这对你重要吗?在我的代码中,我正在测试 DialogResult.Cancel 以确定我的应用程序的退出代码。当使用红色 X 关闭表单时,这被打破了。我现在测试 DialogResult.OK 以指示成功退出。

        return myForm.DialogResult == DialogResult.OK ? 0 : 1;

回答by Uwe Keim

The documentation of the overload

重载文档

public static void Run(
    ApplicationContext context );

has a neat example with a different approach that involves two forms as well.

有一个使用不同方法的简洁示例,它也涉及两种形式。

回答by jswolf19

For a more concerete example of a difference:

有关差异的更具体示例:

If your main form is an MDI form, then the behavior on clicking the close button (the 'x' in the upper right, or Alt-F4) is different depending on which method you use to show the form.

如果您的主窗体是 MDI 窗体,则单击关闭按钮(右上角的“x”或 Alt-F4)的行为会因您使用的显示窗体的方法而异。

With Application.Run(mainForm), the closing event of the child forms run, then the main form's closing event runs.

使用Application.Run(mainForm),子窗体的关闭事件运行,然后主窗体的关闭事件运行。

With mainForm.ShowDialog, the closing event of the main form runs, and the closing event of the child forms do not run.

使用mainForm.ShowDialog,主窗体的关闭事件运行,子窗体的关闭事件不运行。

回答by Tomas Kubes

From my testing I notice that using Application.Run buttons with DialogResult does not close the form (OnFormClosing is not hit) compare to ShowDialog in which the buttons with DialogResult hit OnFormClosing and the close the form.

从我的测试中,我注意到使用带有 DialogResult 的 Application.Run 按钮不会关闭表单(未命中 OnFormClosing)与 ShowDialog 相比,其中带有 DialogResult 的按钮命中 OnFormClosing 并关闭表单。