• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 137
  • Last Modified:

Where do I put functions and procedures I want to run at startup?

Based on command line options I've set my main form to Application.ShowMainForm := False; in the OnCreate event. I now want to process the command line and access functions and procdures that are in the project without showing the main form. I tried to access these functions and procedures from within the OnCreate Event but I get access violations. I assume the errors are because program hasn't fully loaded. Where do I call these functions and procdures from? I tried OnActivate, but it only gets called when the mainform is not hidden.

Thanks
0
ripcord
Asked:
ripcord
  • 3
  • 3
1 Solution
 
erajojCommented:
Hi,
What procedures/functions?
If the procedures/functions you're talking about are in standalone units then make sure you have included them in your project source file's (*.dpr) uses statement and then simply do all the processing there.

Example:

Project source:

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  MyUnit in 'MyUnit.pas'; // here's your unit!
{$R *.RES}

begin
  Application.ShowMainForm := False;
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  if ( MyUnit.CheckParameters ) then
  begin
    Application.ShowMainForm := True;
  end;
  Application.Run;
end.

Standalone unit:

unit MyUnit;

interface

function CheckParameters: Boolean;

implementation

uses
  SysUtils;

function CheckParameters: Boolean;
begin
  Result := CompareText( ParamStr( 1 ), '/hidden' ) <> 0
end;

end.

Explain further...!

/// John
0
 
ripcordAuthor Commented:
I am calling functions and procedures from both the main form(unit) and standalone forms(units). They are the same functions and procdures I normally call when the form is not hidden, so all the units are in my uses statement. It looks like I get the error when one of the procdures attemts to close the application using Close; Is this not allowed from within the OnCreate event or should I be closing in another way?
0
 
ripcordAuthor Commented:
Also when calling a procedure from OnCreate that tries to access ParamCount, the value for ParamCount says "inacessable value".
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
erajojCommented:
Hi,
Use Halt. Close is not available in the OnCreate event.
The reason you can't see ParamCount when debugging is simply because it is a function and not an integer value. It still works (I have tested it).
Sometimes when you get "inacessable value" while debugging is because the awesome object pascal compiler has optimized it out since there is no need for that value at the moment.

procedure TForm1.FormCreate(Sender: TObject);
var
  cParam: Integer;
begin
  cParam := ParamCount;
  Caption := IntToStr( cParam );
  if ( MyUnit.CheckParameters ) then
  begin
    Application.ShowMainForm := True;
  end else
  begin
    Halt;
  end;
end;



0
 
ripcordAuthor Commented:
thanks, it's all working now. Please answer the question to get credit.
0
 
erajojCommented:
Glad to be able to help.

/// John
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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