Solved

Creating run-time component in DLL

Posted on 2004-10-04
8
660 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
  • 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
 
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
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.

 

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

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

20 Experts available now in Live!

Get 1:1 Help Now