?
Solved

Object events in Project Source.

Posted on 2001-06-29
8
Medium Priority
?
151 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Accepted Solution

by:
ITugay earned 100 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

864 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