Solved

Creating run-time component in DLL

Posted on 2004-10-04
8
674 Views
Last Modified: 2010-04-05
Hi!

I have a Delphi DLL project, but I need to create runtime components: How can I, (example) create a TTimer component with events? Please, I don't need the SetTimer or TimerProc API; The TTimer component is just an example.

Thanks.
0
Comment
Question by:nany
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 12

Accepted Solution

by:
Ivanov_G earned 500 total points
ID: 12221664
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    procedure MyOnTimerEvent(Sender : TObject);
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses ExtCtrls;

procedure TForm1.MyOnTimerEvent(Sender : TObject);
begin
  //
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Timer    : TTimer;
begin
  Timer := TTimer.Create(nil);
  Timer.Interval := 1000;
  Timer.OnTimer  := MyOnTimerEvent;
  Timer.Enabled  := True;
end;

end.
0
 

Author Comment

by:nany
ID: 12221735
Thanks Ivanov_G
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12225432
Timer := TTimer.Create(self);

is slightly better. Then the TTimer is connected with the form, thus if the form is freed, the timer gets freed first. Now, you're at a risk that the timer fires an event while the form has been freed, resulting in a nasty access violation or worse. Make sure that whatever component you create in runtime gets freed sooner or later!
0
Technology Partners: 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 12

Expert Comment

by:Ivanov_G
ID: 12225481
Yes, I agree with Workshop_Alex ...

you can use Create(Self) if you have a form. When the form is destroyed, the timer will be freed also.

the other ways - if it is in standalone procedure you can use this:

var
  Timer    : TTimer;
begin
  Timer := TTimer.Create(nil);
  try
    Timer.Interval := 1000;
    Timer.OnTimer  := MyOnTimerEvent;
    Timer.Enabled  := True;
   
    // DO something with the Timer here

  finally
    Time.Free
  end;
0
 

Author Comment

by:nany
ID: 12225733
So, if the component is created inside a DLL (without no form) I must use: Timer := TTimer.Create(nil)?
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12225780
yes,
but make sure after you finished using the timer, you call Timer.Free to release the allocated memory for it.
0
 

Author Comment

by:nany
ID: 12225791
Thanks again Ivanov_G!
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12226681
Actually, you don't just want to free the memory, since it is already freed once you unload the DLL. The main reason for freeing the TTimer is to prevent it from handling any more events. Just imagine what could happen once everything is freed except your timer and your timer shoots another OnTimer event in the air. Just an invitation for some weird access violation that you'll never find. (And which only occurs once every 10 times or so...)
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 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