Title: How can I know if an application running or closing?
Question: How can I know if an application running or closing?
Answer:
To do this,Create a new project and write following codes, so run the project and see the result in the same folder that there is a Program.log file in it.
at the same time,these codes can add in any pas file.if you do it then you must define the name of pas file under uses line on the project source.
-----------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
const PrgLog : String[200] = 'Program.Log';
var
Form1: TForm1;
TXF : TextFile;
implementation
{$R *.DFM}
Procedure STOP;
begin //if Program.log file can not create
MessageDlg( PrgLog+' could not create', mtWarning, [mbOk], 0);
Halt;
end;
Procedure SaveDateTime( fn:String; Alan:String );
begin // write to Program.log file
if not FileExists( PrgLog ) then
begin
System.AssignFile(TXF, PrgLog );
{$I-}System.Rewrite(TXF);{$I+} if IOResult0 then STOP;
System.Writeln(TXF, Application.ExeName+' '+Application.Title );
System.Writeln(TXF, '' );
System.Writeln(TXF, '+ '+DateTimeToStr(Now) );
System.CloseFile(TXF);
end
else
begin
System.AssignFile(TXF, PrgLog );
{$I-}System.Append(TXF);{$I+} if IOResult0 then STOP;
System.Writeln(TXF, alan );
System.CloseFile(TXF);
end;
end;
initialization
begin // application is running
PrgLog := ExtractFileDir(Application.ExeName)+'\'+PrgLog;
SaveDateTime( PrgLog, '+ '+DateTimeToStr(Now) );
end;
finalization
begin // application is closing
SaveDateTime( PrgLog, '- '+DateTimeToStr(Now) );
end;
end.