Board index » cppbuilder » Whats the proper way to terminate an application before all forms are loaded?

Whats the proper way to terminate an application before all forms are loaded?


2003-12-13 09:10:52 AM
cppbuilder55
I have a few forms that are created at design time.
I have a check in my mainform for a condition and if its false,
I need to exit the program, without it loading any other of the
forms that follow in the WinMain routine
I've tried Application->Terminate and it doesn't work.
abort() does work but I don't want any error message and it also
would be nice to do some cleanup if needed.
Whats the best way to do this?
 
 

Re:Whats the proper way to terminate an application before all forms are loaded?

"JM" < XXXX@XXXXX.COM >wrote in message
Quote
I've tried Application->Terminate and it doesn't work.
That is one way to do it. It works fine whenever I use it.
The alternatives are to either 1) throw an exception from the form's
constructor, or 2) alter the project's WinMain() function to test for your
desired condition before any of the forms are created and then simply exit
WinMain() if the condition is encountered.
Quote
abort() does work but I don't want any error message
If you use Abort() (uppercase 'A', not lowercase 'a'), it throws an EAbort
exception. EAbort is a special exception that the VCL looks for and will
not display an error message if encountered.
Quote
and it also would be nice to do some cleanup if needed.
That is your own responsibility to handle before you terminate the
application.
Gambit
 

Re:Whats the proper way to terminate an application before all forms are loaded?

Okay here is what I got to work, hopefully this will be sufficient.
I've set my condition to check for as the first thing that occurs within the
MainForm1's Constructor, so there isn't a problem with cleanup.
----
Application->CreateForm(__classid(TMainForm1), &MainForm1);
if(!Application->Terminated)
{
Application->CreateForm(__classid(TColorSelectForm), &ColorSelectForm);
Application->CreateForm(__classid(TOptionsForm), &OptionsForm);
MainForm1->Show();
Application->Run();
}
else
delete MainForm1;
----
I'd like to use the exceptions so I'm going to try that out now.
On 12/12/2003 7:47 PM Remy Lebeau (TeamB) spoke thusly
Quote
"JM" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...


>I've tried Application->Terminate and it doesn't work.


That is one way to do it. It works fine whenever I use it.

The alternatives are to either 1) throw an exception from the form's
constructor, or 2) alter the project's WinMain() function to test for your
desired condition before any of the forms are created and then simply exit
WinMain() if the condition is encountered.


>abort() does work but I don't want any error message


If you use Abort() (uppercase 'A', not lowercase 'a'), it throws an EAbort
exception. EAbort is a special exception that the VCL looks for and will
not display an error message if encountered.


>and it also would be nice to do some cleanup if needed.


That is your own responsibility to handle before you terminate the
application.


Gambit



 

{smallsort}

Re:Whats the proper way to terminate an application before all forms are loaded?

"JM" < XXXX@XXXXX.COM >wrote in message
Quote
Okay here is what I got to work, hopefully this will be sufficient.
I've set my condition to check for as the first thing that occurs within
the
MainForm1's Constructor, so there isn't a problem with cleanup.
Application->CreateForm(__classid(TMainForm1), &MainForm1);
MainForm1->Show();
You do not need to call Show() yourself, TApplication::Run() will handle
that for you.
Quote
delete MainForm1;
You do not need to free the form yourself, TApplication will handle that
automatically for you during the VCL's own cleanup.
Quote
I'd like to use the exceptions so I'm going to try that out now.
Like I said, you could simply call Abort() from the main form constructor:
__fastcall TMainForm1::TMainForm1(TComponent *Owner)
: TForm(Owner)
{
if( some condition )
Abort();
//...
}
Throwing an exception from a constructor will automatically clean up the
form instance (assuming you haven't already allocated any resources
yourself - you will be responsble for freeing those), and then the exception
will bypass the rest of the form constructors altogether and exit WinMain()
immediately, without an error message.
Otherwise, since you are already modifying WinMain() anyway, just do your
check before creating the MainForm:
if( some condition is not found )
{
Application->CreateForm(__classid(TMainForm1), &MainForm1);
Application->CreateForm(__classid(TColorSelectForm),
&ColorSelectForm);
Application->CreateForm(__classid(TOptionsForm), &OptionsForm);
Application->Run();
}
Gambit