Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 695
  • Last Modified:

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
0
TG-Steve
Asked:
TG-Steve
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Geert GruwezOracle dbaCommented:
when the procedure is finished call

application.Terminate

or close the main form
0
 
jimyXCommented:
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
0
 
Ephraim WangoyaCommented:
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
TG-SteveAuthor Commented:
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
0
 
TG-SteveAuthor Commented:
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
0
 
TG-SteveAuthor Commented:
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

0
 
Ephraim WangoyaCommented:
That is correct
0
 
Geert GruwezOracle dbaCommented:
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
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now