views:

650

answers:

5

after alot of head scathing i think I have made some progress. I am not 100% sure but Can you exit an application before the constructor is finsihed and the main form loaded?

constuctor { thread t = new thread(open loading screen); }

I do the exact same thing with a exit screen using a variable between the main for and another one. and I have an application exit in the main for if it returns true.

At the start I have a loading screen but the main form isnt loaded yet and the call has been made from the constructor and th econstrutor hasnt finished yet.

And one more thing should all the thread/ calss / loading // program setup be done in the main constructor or some other way ifso please advise .

thanks

A: 

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.

Lasse V. Karlsen
A: 

I mean after the prorgam.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 the app1() the right place to initialzie everything
  2. if I tried and send a message back from loading screen beforw app1() is finished to close it doesnt work the process still runs even through nothing is open.
A: 

When main thread ends:

  • background threads are "killed/abandoned"
  • foreground threads (the default when creating threads) are waited till they finish.
Petar Repac
A: 

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)
  {
    //..
  }
}
abatishchev
+2  A: 

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.

jontsnz