Solved

Creating run-time component in DLL

Posted on 2004-10-04
8
655 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
Comment Utility
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
Comment Utility
Thanks Ivanov_G
0
 
LVL 17

Expert Comment

by:Wim ten Brink
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:nany
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks again Ivanov_G!
0
 
LVL 17

Expert Comment

by:Wim ten Brink
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

18 Experts available now in Live!

Get 1:1 Help Now