Link to home
Start Free TrialLog in
Avatar of TG-Steve
TG-Steve

asked on

delphi application run procedure then application exit

Hi guys

within my application i have several buttons of which each of them export data from a local mdb and populate tables in a remote mysql database.

i am after trying to create a scheduled task to run my program in the early hours. so require the app to open then button1.click; button2.click; then completely exit the application untill it runs next.

i have tried several different methods within the OnCreate and although my form loads but not visible untill the procedures complete i am ok with this but its the exiting which is the problem.

could i have a couple of ideas of how to perform this best (i was going to question how to create sceduled task with parameters) but i think this may get long winded i.e myapp.exe -'export' but i think that will require more indepth research

thank you in advance
Avatar of Geert G
Geert G
Flag of Belgium image

when the procedure is finished call

application.Terminate

or close the main form
Avatar of jimyX
jimyX

I do not understand what is your requirement(s) here your question is a little bit not clear or there is no direct question.
If you want to schedule a time for your application to run at, then use the Win Scheduled Tasks and set your time (pic attached) and you can put this code on your form at creation:
procedure TForm1.FormCreate(Sender: TObject);
begin
  Button1.Click;
  Button2.Click;
  Sleep(12000);// or any suitable time (in ms)
  Application.Terminate;
end;

Open in new window


Or you can make your application runs when Win starts and keeps running in the background (in the system tray for instance or completely hidden) and your application can call a set of instructions at a specific time that you set and hence no need to use the Win Scheduled Tasks at all.

Waiting to hear from you.
ScheduledTask.JPG
Here is some code you can use. The TAutoRunParams should ideally be in its own unit.
If you start the application with AutoRun = True then it automatically does the action you requested otherwise it opens normally

Set the schedule as shown here
unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;


const
  CParamAutoRun = 'AUTORUN';
  CParamImport = 'IMPORT';
  CParamExport = 'EXPORT';
  CParamYes = 'YES';
  CParamNo = 'NO';

type
  TRunParams = class(TStringList)
  public
    procedure LoadParams;
    function ParamByName(const AName: string): string;
  end;

  TForm3 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    FRunParams: TRunParams;
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

{ TRunParams }

procedure TRunParams.LoadParams;
var
	I: Integer;
  Prm: string;
begin
  for I := 1 to ParamCount do
  begin
    Prm := UpperCase(Trim(ParamStr(I)));
    if Prm[1] = '"' then
      Prm := Copy(Prm, 2, Length(Prm) -1);
    if Prm[Length(Prm)] = '"' then
      SetLength(Prm, Length(Prm) -1);

    Prm := StringReplace(Prm, '\\', '\', [rfReplaceAll]);

    if IndexOf(Prm) < 0 then
      Add(Prm);
  end;
end;

function TRunParams.ParamByName(const AName: string): string;
begin
  Result := Values[AName];
end;

 { TForm3 }

procedure TForm3.Button1Click(Sender: TObject);
begin
  //
end;

procedure TForm3.Button2Click(Sender: TObject);
begin
//
end;

procedure TForm3.FormCreate(Sender: TObject);
begin
  FRunParams := TRunParams.Create;
  FRunParams.LoadParams;
end;

procedure TForm3.FormDestroy(Sender: TObject);
begin
  FreeAndNil(FRunParams);
end;

procedure TForm3.FormShow(Sender: TObject);
begin
  if FRunParams.ParamByName(CParamAutoRun) = CParamYes then
  begin
    if FRunParams.ParamByName(CParamImport) = CParamYes then
    begin
      //its much better to use functions rather than call button click events directly
      Button1Click(nil);
      Button2Click(nil);
    end;

    PostMessage(Handle, WM_CLOSE, 0, 0); //use post message
  end;
end;

end.

Open in new window

Schedule1.png
Avatar of TG-Steve

ASKER

Jimyx

i was using the same procedure you outlined above by launching the app from windows scheduled task but my app saves a log file log.txt which when i run the procedure from within a button in my app it works perfect. but if run from scheduled task then i see a hidden window with an error accessing system32\log.txt which is strange because it works fine and saves the file in the same folder as the app if run the first way. i have now rectified this and instead put the actual directory to save the file to within the code. it is good to see i used the same principle as what you have mentioned though and that i am not doing it wrong

thank you
ewangoya:

this is very interesting and i am just trying this method because this may save me a lot of time. i was going to make 3 different applications. one to do a full refresh of the webserver inventory in the early hours. one to do incremental and one to pull data back (sales) but you may have just saved me days of work by using the one application to do all 3.

thank you and i will report back as soon as i can
I have split the code into a seperate unit. can i just confirm that the first snippet of code is what sits in the second pas file and the rest i have added this into my uses with the rest of the code? i have tested and it seems to work perfectly. but just want to make sure its correct

unit param;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;


const
  CParamAutoRun = 'AUTORUN';
  CParamImport = 'IMPORT';
  CParamExport = 'EXPORT';
  CParamYes = 'YES';
  CParamNo = 'NO';

type
  TRunParams = class(TStringList)
  public
    procedure LoadParams;
    function ParamByName(const AName: string): string;
  end;

implementation

{ TRunParams }

procedure TRunParams.LoadParams;
var
	I: Integer;
  Prm: string;
begin
  for I := 1 to ParamCount do
  begin
    Prm := UpperCase(Trim(ParamStr(I)));
    if Prm[1] = '"' then
      Prm := Copy(Prm, 2, Length(Prm) -1);
    if Prm[Length(Prm)] = '"' then
      SetLength(Prm, Length(Prm) -1);

    Prm := StringReplace(Prm, '\\', '\', [rfReplaceAll]);

    if IndexOf(Prm) < 0 then
      Add(Prm);
  end;
end;

function TRunParams.ParamByName(const AName: string): string;
begin
  Result := Values[AName];
end;

end.

Open in new window

private
    FRunParams: TRunParams;


procedure TForm3.FormCreate(Sender: TObject);
begin
  FRunParams := TRunParams.Create;
  FRunParams.LoadParams;
end;

procedure TForm3.FormDestroy(Sender: TObject);
begin
  FreeAndNil(FRunParams);
end;

procedure TForm3.FormShow(Sender: TObject);
begin
  if FRunParams.ParamByName(CParamAutoRun) = CParamYes then
  begin
    if FRunParams.ParamByName(CParamImport) = CParamYes then
    begin
      //its much better to use functions rather than call button click events directly
      Button1Click(nil);
      Button2Click(nil);
    end;

    PostMessage(Handle, WM_CLOSE, 0, 0); //use post message
  end;
end;

end.

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you seem to be forgetting the main problem

on the security tab, you can run with system user or not
check the checkbox run with highest priviliges
that should solve your problem