Recent

Author Topic: Why wont Lazarus exit?  (Read 9880 times)

georgelappies

  • New Member
  • *
  • Posts: 35
Why wont Lazarus exit?
« on: June 30, 2011, 10:24:36 am »
Hi All

First thanks for providing us with this excellent software. I have a problem however. When my app starts up it compares its version against a field in a MySQL db. If not the same the app should shutdown after displaying a message.

Everything works fine except for the app shutting down, it just continues on past the 'Close;' statement...

Below is the code
Code: [Select]
procedure TfrmLogin.FormCreate(Sender: TObject);
{
 Need code here to compare the version of the app with the latest one
 on the database, if client is not the same error box will come up and
 display location to download latest version and then exit application.
}
var
  sqlVersion: String;

begin
  // Check if we have an active connection. If so, let's close it.
  if MySQLConnection1.Connected then
     begin
          // The SQLTransaction1 gets activated automatically, but before we can close
          // the connection we have to set the SQLTransaction1.Active to false.
          SQLTransaction1.Active := False;
          MySQLConnection1.Close;
     end;

  // Create the sql statement
  sqlVersion := 'select idversion from sasol_kpa.version where version=''' +
                APP_VERSION + '''';

  // Set the connection parameters.
  MySQLConnection1.HostName := DB_HOSTNAME;
  MySQLConnection1.UserName := DB_USER;
  MySQLConnection1.Password := DB_PASSWD;
  MySQLConnection1.DatabaseName := DB_NAME;
  // Open the connection.
  MySQLConnection1.Open;

  // Execute the query
  if MySQLConnection1.Connected then
     begin
          SQLQuery1.SQL.Text := sqlVersion;
          SQLQuery1.Open;
          if (SQLQuery1.IsEmpty) then
             begin
                  MessageDlg('Error! You need to update your app from \\secdapp30\',mtError, mbOKCancel, 0);
                  Close;
             end;
          ShowMessage('Version Correct!');
     end;
end; 

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12712
  • FPC developer.
Re: Why wont Lazarus exit?
« Reply #1 on: June 30, 2011, 10:52:38 am »
Close (depending on scope) probably closes the form.  Use application.terminate or some other method from the "application" object (see docs)

georgelappies

  • New Member
  • *
  • Posts: 35
Re: Why wont Lazarus exit?
« Reply #2 on: June 30, 2011, 11:06:46 am »
Close (depending on scope) probably closes the form.  Use application.terminate or some other method from the "application" object (see docs)

Thanks

Code: [Select]
application.terminate
worked perfectly!

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Why wont Lazarus exit?
« Reply #3 on: June 30, 2011, 11:27:19 am »
Application.Terminate should propably only be used in rare occasions? What i guess is it ignore all close events etc and just brute force closes application.

You have frmLogin from which you call Close. Is it the mainform of the application? If not, you should call close from the mainform, as that should close the application too.

edit: However i see that you try to exit in formCreate. I did simple test just to make sure...

This application will not close itself, you have to do it from elsewhere than formCreate, such as Timer that starts running after formCreate and ticks just that once:
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
begin
  close;
end;
« Last Edit: June 30, 2011, 11:31:12 am by User137 »

georgelappies

  • New Member
  • *
  • Posts: 35
Re: Why wont Lazarus exit?
« Reply #4 on: June 30, 2011, 12:31:46 pm »
Application.Terminate should propably only be used in rare occasions? What i guess is it ignore all close events etc and just brute force closes application.

You have frmLogin from which you call Close. Is it the mainform of the application? If not, you should call close from the mainform, as that should close the application too.

edit: However i see that you try to exit in formCreate. I did simple test just to make sure...

This application will not close itself, you have to do it from elsewhere than formCreate, such as Timer that starts running after formCreate and ticks just that once:
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
begin
  close;
end;

Thanks I will look into it.

 

TinyPortal © 2005-2018