Solved

Object events in Project Source.

Posted on 2001-06-29
8
140 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Filemaker 14 vs Delphi Embarcadero 7 185
indy Tmemory stream non latin charcter issue 2 97
Delphi application Soap connection 5 94
Find and Replace Stream with 0s 8 56
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

930 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

12 Experts available now in Live!

Get 1:1 Help Now