什么是Application.Run()和Form.ShowDialog()之间的区别? [英] What's the difference between Application.Run() and Form.ShowDialog()?

查看:1248
本文介绍了什么是Application.Run()和Form.ShowDialog()之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我想先说明一个登录表单的主要形式。如果登录已经成功。目前,我做的是这样的:

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

但后来我开始怀疑 - 什么是点的 Application.Run()?为什么不只是做(新的MainForm())。ShowDialog的()呢?有什么不同?什么将是达到我想要的正确方法?

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(表格)启动当前线程上的消息循环并显示指定的形式。消息循环使形式来接收Windows消息(例如,关键presses,鼠标点击,油漆失效),允许它出现响应,并与用户交互。当你调用的ShowDialog()表格实例,它实际上做了类似的事情,并创建一个模式消息循环表格上的ShowDialog 被调用。

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 Form instance, it actually does a similar thing and creates a modal message loop for the form on which ShowDialog has been called.

有没有在两个电话之间太大的区别。 Application.Run 确实增加了一些额外的事件处理让你做一些整理资源,当主窗体关闭(请参阅Application.ThreadExit).

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

开始的WinForms应用程序推荐的方法是使用 Application.Run ,但我怀疑这是一个多规则的公约。如果你要打开多个非模态的形式使用的最大原因 Application.Run 是。为此,可以使用这样做:

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.Run is if you want to open multiple non-modal forms. You can do this using:

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

您不能使用的ShowDialog()方法的一种形式必须是模式的实现这一点。

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

至于你如何显示登录表单,然后如果登录成功的主要形式问题,我觉得你有什么是好的:

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

另一种方法是自己做的管道,并在收盘时打开的MainForm 的实例 LoginForm的如果登录成功。

这篇关于什么是Application.Run()和Form.ShowDialog()之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆