Solved

Object events in Project Source.

Posted on 2001-06-29
8
139 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
  • 4
  • 3
8 Comments
 
LVL 9

Expert Comment

by:ITugay
Comment Utility
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
Comment Utility


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
Comment Utility
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
 
LVL 9

Accepted Solution

by:
ITugay earned 50 total points
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:MoonCalf
Comment Utility
I'll give it a go.

Thanks.
0
 
LVL 6

Expert Comment

by:edey
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now