Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Creating run-time component in DLL

Posted on 2004-10-04
8
Medium Priority
?
684 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

636 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