Solved

delphi application run procedure then application exit

Posted on 2011-02-26
8
654 Views
Last Modified: 2012-05-11
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
Comment
Question by:TG-Steve
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 34988462
when the procedure is finished call

application.Terminate

or close the main form
0
 
LVL 24

Expert Comment

by:jimyX
ID: 34988587
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
 
LVL 32

Expert Comment

by:ewangoya
ID: 34989021
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
 

Author Comment

by:TG-Steve
ID: 34989321
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:TG-Steve
ID: 34989332
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
 

Author Comment

by:TG-Steve
ID: 34989568
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
 
LVL 32

Accepted Solution

by:
ewangoya earned 250 total points
ID: 34989589
That is correct
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 34990045
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now