Solved

Object events in Project Source.

Posted on 2001-06-29
8
144 Views
Last Modified: 2010-04-06
I've created a small application, purely in the Project Source, not using any forms.

How, within this same file, can I handle the OnTimer event of a timer created at runtime?

Thanks,

MoonCalf.
0
Comment
Question by:MoonCalf
[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
  • 4
  • 3
8 Comments
 
LVL 9

Expert Comment

by:ITugay
ID: 6238445
Hi MoonCalf,
to keep your resources clean take a look at Windows API functions
SetTimer and KillTimer.

-----
Igor.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6238464


sample:

procedure SomeTimerEvent(H: hwnd; Msg, ID: Integer; Time: DWord); stdcall;
begin
  Form1.Caption := IntToStr(Time);
end;

var
  TimerID: Integer;

initialization
  TimerID := SetTimer(0, 1, 500, @SomeTimerEvent);
finalization
  KillTimer(0, TimerID);
end.

-----
Igor.
0
 
LVL 1

Author Comment

by:MoonCalf
ID: 6238475
This is purely an example.  If I know how to handle the OnTimer event of a TTimer, I can handle any of the events of other objects as well.

It's the idea of handling the events that I need to understand, not any specific ones.  Maybe a TTimer was a bad one to choose.

MoonCalf.
0
Independent Software Vendors: 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!

 
LVL 9

Accepted Solution

by:
ITugay earned 50 total points
ID: 6238498
Ok,
then you can create an "empty" object to prvide an interface part for "object" method, something like this

type
  TDummyObject = class(TObject)
     procedure SomeEvent(Sender: TObject);
  end;



procedure TDummyObject.SomeEvent(Sender: TObject);
begin
  // do something
end;

var
  Dummy: TDummyObject;
....
  Dummy := TDummyObject.Create;
  Timer1.OnTimer := Dummy.SomeEvent;


-----
Igor.


PS: if you'd like to avoid of dummy object then let me know :-)

 


0
 
LVL 1

Author Comment

by:MoonCalf
ID: 6238501
I'll give it a go.

Thanks.
0
 
LVL 6

Expert Comment

by:edey
ID: 6238505
you could try something like this:

uses ... extctrls;

type
 TMyDummyObject = class(TObject)
 public
  procedure doTimer(Sender : TObject);//event handlers need to be methods
 end;

var
 dummy : TDummyObject;
 timer : TTimer;

procedure TDummyObject.doTimer(sender : TObject);
begin
 ...
end;

begin

 ...

 dummy : TDummyObject.create;
 timer := TTimer.create(some_parent_object);
 timer.onTimer := dummy.doTimer;

 ...

 timer.enabled := false;
 dummy.free;
end;

GL
Mike
0
 
LVL 1

Author Comment

by:MoonCalf
ID: 6238533
Thanks for the answer, but it took a bit of "rewording" to get it to work correctly.

MoonCalf.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6238709
hi MoonCalf,

this is the "bonus track" for grade "C".
No dummy objects, just not typesafe code.

var
  Timer: TTimer;

procedure OnTimer(Self, Sender: TObject); register;
begin
  // do something here
  // e.g.  Form1.Caption := IntToStr(GetTickCount);
end;

begin

  Timer := TTimer.Create(nil);
  pointer((@@Timer.OnTimer)^):= @OnTimer;
  Timer.Interval := 100;
 
  .....
  Timer.Free;
end.

Good wishes,
Igor.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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