Can I close the program from the main designer?

Can you exit the application before completing the constructor and upload the main form?

At startup, I have a loading screen that appears before the main form loads. The loading screen is displayed from the designer until the designer completes.

I am doing something similar with an exit screen 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 the entire thread / class / loading / configuring the program be started in the main constructor, or am I doing it wrong?

Update:

I mean after 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 the app1()right place to initialize everything?
  • "" app1(), - , .
+3
6

, , , ( , ), Application.Exit() , Environment.Exit(-1) .

+10

,


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

...

+5

?

Main, Program?

, , .

, , , , - - , , , .

Main , - , .

, , , , , .

Main , .

0

:

  • "/"
  • ( ) .
0

. , .

class ConstructorAbortedException : Exception { }

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

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

jontsnz,

Environment.Exit(-1)

, "Application Hang", Windows.

Environment.Exit(0)

, , .

0

Source: https://habr.com/ru/post/1706097/


All Articles