Solved

delphi application run procedure then application exit

Posted on 2011-02-26
8
663 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 37

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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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
 

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 37

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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