我可以从 Main 构造函数中关闭程序吗?
您可以在构造函数完成并加载主窗体之前退出应用程序吗?
启动时,我有一个加载屏幕,在加载主窗体之前显示。 在构造函数完成之前,加载屏幕会从构造函数中显示。
我通过在主窗体和退出屏幕之间使用变量来对退出屏幕执行类似的操作。 如果退出屏幕返回 true,我会在主窗体中退出应用程序。
最后,所有线程/类/加载/程序设置都应该在主构造函数中完成还是我做错了?
更新:
我的意思是在program.cs之后和静态main中
namespace app
{
public partial class app1 : Form
{
public app1()
{
InitializeComponent();
// open loading screen
// initialize vars
// create objects
}
// form opens when app1() finishes
app1()
是初始化所有内容的正确位置吗?- 如果我尝试在
app1()
完成之前从加载屏幕发送回“关闭”消息,则它不起作用 - 即使没有任何内容打开,该进程仍会运行。
Can you exit an application before the constructor is finished and the main form is loaded?
At startup, I have a loading screen that displays before the main form is loaded. The loading screen is displayed from the constructor before the constructor has finished.
I do something similar with an exit screen by using a variable between the main form and the exit screen. I have an application exit in the main form if the exit screen returns true.
Finally, should all the thread/class/loading/program setup be done in the main constructor or am I doing it wrong?
Update:
I mean after the program.cs and in the static main
namespace app
{
public partial class app1 : Form
{
public app1()
{
InitializeComponent();
// open loading screen
// initialize vars
// create objects
}
// form opens when app1() finishes
- Is
app1()
the right place to initialize everything? - If I try to send a "close" message back from the loading screen before
app1()
is finished, it doesn't work - the process still runs even though nothing is open.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我发现,如果我尝试从主窗体构造函数终止我的应用程序,而我仍然在另一个线程上显示启动屏幕(看起来与您正在做的类似),则
Application.Exit()< /code> 不起作用,但
Environment.Exit(-1)
起作用。I've found that if I try to kill my application from the main form constructor when I still have the splash screen showing on a different thread (which looks similar to what you are doing), that
Application.Exit()
does not work, butEnvironment.Exit(-1)
does.试试这个,
它会很好用...
try this,
it will work well...
哪个类的哪个主构造函数?
您是在谈论在
Program
类中具有默认位置的静态方法Main
吗?您可以使用该方法来执行在屏幕上打开任何窗口之前需要进行的初始化。
显然,如果您需要使用加载屏幕,您可能需要将一些代码移动到其他地方,因为您需要围绕表单的消息循环,并且消息循环将阻塞,直到表单关闭。
如果在打开任何表单之前从 Main 方法返回,则不会明显显示任何表单。
说了这么多,我觉得你的问题有点模糊,我很确定我不明白你到底在问什么。
首先也是最重要的,
Main
不是构造函数,它只是一个静态方法。Which main constructor of which class?
Are you talking about the static method
Main
that has a default location in theProgram
class?You use that method to do initialization that needs to occur before you open any windows on screen.
Obviously, if you need to use a loading screen, you will probably want to move some code somewhere else, as you need a message loop around forms, and the message loop will block until your form closes.
If you return from the Main method before you open any form, then no form will be shown obviously.
Having said all that, I feel your question is a bit vague and I'm pretty sure I didn't understand exactly what it is that you're asking.
First and foremost,
Main
is not a constructor, it's just a static method.当主线程结束时:
When main thread ends:
您只能通过抛出异常来破坏构造函数。 要秘密地执行此操作,请抛出您自己的特定异常。
you can break constructor only via throwing an exception. To do that surreptitiously, throw you own specific exception.
正如 jontsnz 回答的那样,代码为
环境.退出(-1)
在构造函数中工作正常,但这会导致应用程序抛出“应用程序挂起”事件,这在 Windows 事件查看器中可以视为错误。 使用
环境.退出(0)
但退出时不会注册错误,所以我更喜欢那个。
As jontsnz answered, the code with
Environment.Exit(-1)
works fine in the constructor, but this causes the application to throw an "Application Hang" event, which can be seen as an error in the Windows Event Viewer. Using
Environment.Exit(0)
exits without registering an error though, so I prefer that one.