Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Handling Object events within project source.

Posted on 2000-04-18
5
Medium Priority
?
163 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

721 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