Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

terminate application if condition not met 2

Status
Not open for further replies.

AP81

Programmer
Apr 11, 2003
740
AU
Hey guys,

Just wondering if anyone can help me out here. I have a procedure (shown below)which is called on the FormCreate event. This checks if it can connect to a database.

If it can't connect to it, I display a warning and close the application. Now here is the problem. The application closes, but if you ctrl+alt+delete it is still listed as a running process. I have tried using .close and Application.Terminate methods and can't think of anything else. I could possibly put a try-catch where it calls the procedure, but I doubt this will work either.

Any ideas?

Thanks heaps,
Adam



Code:
//sets the database connection string
procedure TfrmMain.SetConnectionString;
begin
  try
    //set connection string
    connString :=
            'Provider=MSDASQL;' +
            'DRIVER={MySQL ODBC 3.51 Driver};' +
            'SERVER=QBserver' +    //QBServer (or localhost for testing)
            ';DATABASE=quicken' +
            ';UID=root' +
            ';PASSWORD=';
  except on E:Exception do
    begin
      MessageDlg('ODBC connnection failed.' + #13#10 +
                 #13#10 +
                 'Check ODBC 3.51 driver is installed.' ,
                 mtWarning, [mbOK], 0);
      close; //close the application
      //exit; //exit procedture
    end;
  end;  {try}

  //establish a directSQL connection to the database
  if MySQLClient.connect('QBServer','root','','quicken',3306,'',false,0) then
      frmMain.Caption := frmMain.caption + ' ' +
                        '  --> Database connection established <--'
  else
    begin
      MessageDlg('DirectSQL connnection failed.' + #13#10 +
                 #13#10 +
                 'Application will now close.' ,
                 mtWarning, [mbOK], 0);
      close; //close the application
    end;
end;




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
Adam:

I put together a little test:

procedure TForm1.FormCreate(Sender: TObject);
begin
MessageDlg('Going to quit',
mtWarning, [mbOK], 0);
Halt;
end;

And it's not there in the task manager afterward.

Regards and HTH,
JGS
 
Thank you! I thought I'd have to result in making a WinAPI call to terminate the application. I can't believe it was that simple.

A star for you.




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
I'm using Halt in some cases also so this is just a little note.

Halt terminates the program abnormally and in some cases this might cause more problems. So if you have for example files open in your program, close them before Halt. Otherwise they might be in use the next time you try to access them.

Be careful and yuo'll be alright :)
 
I have had this problem in Delphi 1 - it seems to be timing related. Try sending a close message instead of halting:

PostMessage(Form1.handle,WM_CLOSE,0,0);
 
AP81:

Thank you for your appreciation.

Although other posts have suggested alternative methods of "termination", one must stop to realize that during FormCreate, "we're not all there yet".

Yes, abrupt (I'm an ol' mainframer so we call it abend) termination of the program can cause some lingering problems, I always find it easier to solve MY (open file, etc) problem than to find out why the &^%$# program won't go away .. it may be brutal, but it "work-ith"

Best Regards,
JGS
 
and a simple Application.Terminate; wont work? Always works fine for me.

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
[/b]
Great Delphi Websites faq102-5352
 
Thanks for your help guys.

I think I will be sticking with the 'Halt;' call. No datasets, etc are connected until after my procedure so it should work alright.

geoclock, I'll have a tinker with your suggestion:

PostMessage(Form1.handle,WM_CLOSE,0,0);

I'd be interested to see if the PostMessage call executes the OnClose & OnDestroy form methods.

Thanks all again.




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
Here is the general structure of the .dpr that I use:
Code:
program MyProgram;

uses
  Forms, 
  MyDataModule in 'MyDataModule.pas' {DM: TDataModule},
  Main in 'Main.pas' {frmMain},
  :
  :

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TDM, DM);
  // Don't show main form if user didn't log in.
  if DM.Connected then
    begin
      Application.CreateForm(TfrmMain, frmMain);
      :
      : 
    end;
  Application.Run;
end.
DM is the only form created by Delphi (see Project/Options/Forms), and there is no pre-defined "Main form". By having a property "Connected" in the DM object to test, if the connection was not made for any reason, frmMain is not created. When delphi runs and doesn't find a form to open, it quietly ends. It also avoids "noise" on the screen when the form appears for an instant and then goes away.

You do use a separate data module from the user interface, don't you?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top