Solved

Handling Object events within project source.

Posted on 2000-04-18
5
155 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
  • 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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