我可以从 Main 构造函数中关闭程序吗?

发布于 2024-07-16 19:48:04 字数 654 浏览 6 评论 0原文

您可以在构造函数完成并加载主窗体之前退出应用程序吗?

启动时,我有一个加载屏幕,在加载主窗体之前显示。 在构造函数完成之前,加载屏幕会从构造函数中显示。

我通过在主窗体和退出屏幕之间使用变量来对退出屏幕执行类似的操作。 如果退出屏幕返回 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
  1. app1() 是初始化所有内容的正确位置吗?
  2. 如果我尝试在 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
  1. Is app1() the right place to initialize everything?
  2. 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 技术交流群。

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

发布评论

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

评论(6

时光病人 2024-07-23 19:48:04

我发现,如果我尝试从主窗体构造函数终止我的应用程序,而我仍然在另一个线程上显示启动屏幕(看起来与您正在做的类似),则 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, but Environment.Exit(-1) does.

虫児飞 2024-07-23 19:48:04

试试这个,


    public partial class MyForm : Form
    {        
    public MyForm()
    {
     if (MyFunc())
        {
            this.Shown += new EventHandler(MyForm_CloseOnStart);
        }
    }

    private void MyForm_CloseOnStart(object sender, EventArgs e)
    {
         this.Close();
    }
    }

它会很好用...

try this,


    public partial class MyForm : Form
    {        
    public MyForm()
    {
     if (MyFunc())
        {
            this.Shown += new EventHandler(MyForm_CloseOnStart);
        }
    }

    private void MyForm_CloseOnStart(object sender, EventArgs e)
    {
         this.Close();
    }
    }

it will work well...

原来是傀儡 2024-07-23 19:48:04

哪个类的哪个主构造函数?

您是在谈论在 Program 类中具有默认位置的静态方法 Main 吗?

您可以使用该方法来执行在屏幕上打开任何窗口之前需要进行的初始化。

显然,如果您需要使用加载屏幕,您可能需要将一些代码移动到其他地方,因为您需要围绕表单的消息循环,并且消息循环将阻塞,直到表单关闭。

如果在打开任何表单之前从 Main 方法返回,则不会明显显示任何表单。

说了这么多,我觉得你的问题有点模糊,我很确定我不明白你到底在问什么。

首先也是最重要的,Main 不是构造函数,它只是一个静态方法。

Which main constructor of which class?

Are you talking about the static method Main that has a default location in the Program 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.

七禾 2024-07-23 19:48:04

当主线程结束时:

  • 后台线程被“杀死/放弃”,
  • 前台线程(创建线程时的默认设置)将等待直到完成。

When main thread ends:

  • background threads are "killed/abandoned"
  • foreground threads (the default when creating threads) are waited till they finish.
对不⑦ 2024-07-23 19:48:04

您只能通过抛出异常来破坏构造函数。 要秘密地执行此操作,请抛出您自己的特定异常。

class ConstructorAbortedException : Exception { }

class Foo
{
  public Foo()
  {
    if(goesWrong)
    {
      throw new ConstructorAbortedException();
    }
  }
}

void Bar()
{
  try
  {
    Foo f = new Foo();
  }
  catch(ConstructorAbortedException)
  {
    //..
  }
}

you can break constructor only via throwing an exception. To do that surreptitiously, throw you own specific exception.

class ConstructorAbortedException : Exception { }

class Foo
{
  public Foo()
  {
    if(goesWrong)
    {
      throw new ConstructorAbortedException();
    }
  }
}

void Bar()
{
  try
  {
    Foo f = new Foo();
  }
  catch(ConstructorAbortedException)
  {
    //..
  }
}
完美的未来在梦里 2024-07-23 19:48:04

正如 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.

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