Solved

Handling Object events within project source.

Posted on 2000-04-18
5
158 Views
Last Modified: 2010-04-04
I've got a simple project (NO forms) and I've got a TTimer in it.  How do I handle the events within the Project's source?

John.
0
Comment
Question by:Jaymol
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:shenqw
ID: 2727533
//Unit1

unit Unit1;

interface

uses
  extctrls,
  Forms;

type
  MyTest=class(TObject)
  private
    Text:TextFile;
    bExit:boolean;
    iCount:integer;
    Timer:TTimer;
    procedure TimerEvent(Sender:TObject);
  public
    constructor Create;
    destructor  Destroy;override;
    procedure Run;
  end;

var
  Test:MyTest;

implementation

{ MyTest }

constructor MyTest.Create;
begin
  inherited;
  bExit:=False;
  iCount:=0;
  AssignFile(Text,'c:\Temp\Test.Text');
  Rewrite(Text);
  Timer:=TTimer.Create(nil);
  Timer.OnTimer:=TimerEvent;
  Timer.Interval:=1000;
  Timer.Enabled:=True;
end;

destructor MyTest.Destroy;
begin
  CloseFile(Text);
  Timer.Free;Timer:=nil;
  inherited;
end;

procedure MyTest.Run;
begin
  while not bExit do Application.ProcessMessages;
end;

procedure MyTest.TimerEvent(Sender: TObject);
begin
  Inc(iCount);
  writeln(Text,iCount);
  if iCount>100 then bExit:=True;
end;

end.


//Main Unit

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas';

{$R *.RES}

begin
  Application.Initialize;
  Test:=MyTest.Create;
  Test.Run;
end.
0
 
LVL 6

Author Comment

by:Jaymol
ID: 2729744
Thanks for the suggestion Shenqw, but that's not what I want.

I want to handle the events within the project source - not elsewhere.

Thanks,

John.
0
 
LVL 3

Accepted Solution

by:
shenqw earned 50 total points
ID: 2729835
//try follow

program Project2;

uses
  extctrls,
  Forms;

type
  MyTest=class(TObject)
  private
    Text:TextFile;
    bExit:boolean;
    iCount:integer;
    Timer:TTimer;
    procedure TimerEvent(Sender:TObject);
  public
    constructor Create;
    destructor  Destroy;override;
    procedure Run;
  end;

var
  Test:MyTest;

{$R *.RES}

{ MyTest }

constructor MyTest.Create;
begin
  inherited;
  bExit:=False;
  iCount:=0;
  AssignFile(Text,'c:\Temp\Test.txt');
  Rewrite(Text);
  Timer:=TTimer.Create(nil);
  Timer.OnTimer:=TimerEvent;
  Timer.Interval:=100;
  Timer.Enabled:=True;
end;

destructor MyTest.Destroy;
begin
  CloseFile(Text);
  Timer.Free;Timer:=nil;
  inherited;
end;

procedure MyTest.Run;
begin
  while not bExit do Application.ProcessMessages;
end;

procedure MyTest.TimerEvent(Sender: TObject);
begin
  Inc(iCount);
  writeln(Text,iCount);
  if iCount>100 then bExit:=True;
end;

begin
  Application.Initialize;
  Test:=MyTest.Create;
  Test.Run;
  Test.Free;
end.


0
 

Expert Comment

by:dentener
ID: 2729854
Try something like this:

1. Create a small object withing your project source and add the method below:

procedure MessageHandler(var Msg: TMsg; var Handled: Boolean);

2. In your projectsource add:

SetTimer(Application.Handle, 1, INTERVAL, nil);

3. Create the small object and connect Application.OnMessage with the MessageHandler method

4. Add some code like the code below to the MessageHandler method

  with Msg do
    if Msg = WM_TIMER then
    begin
      Handled:= true;
      ......
    end
    else handled:= false;

5. Finnaly add KillTimer(Application.Handle, 1) somewhere after Application.Run;


I Really don't know if this works, you will have to try it...

Regards,

Paul
0
 
LVL 6

Author Comment

by:Jaymol
ID: 2730095
Thanks Shenqw.  That's just what I was after!

Thanks everyone else for your input, but Shenqw gave the best answer 1st.

Ta,

John.
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.

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…

710 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