Solved

Object events in Project Source.

Posted on 2001-06-29
8
141 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

815 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

10 Experts available now in Live!

Get 1:1 Help Now