• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 689
  • Last Modified:

Creating run-time component in DLL

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
nany
Asked:
nany
  • 3
  • 3
  • 2
1 Solution
 
Ivanov_GCommented:
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
 
nanyAuthor Commented:
Thanks Ivanov_G
0
 
Wim ten BrinkCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Ivanov_GCommented:
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
 
nanyAuthor Commented:
So, if the component is created inside a DLL (without no form) I must use: Timer := TTimer.Create(nil)?
0
 
Ivanov_GCommented:
yes,
but make sure after you finished using the timer, you call Timer.Free to release the allocated memory for it.
0
 
nanyAuthor Commented:
Thanks again Ivanov_G!
0
 
Wim ten BrinkCommented:
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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now