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
Solved

Using TTimer in Threads - OnTimer event never fires.

Posted on 2000-04-25
4
1,071 Views
Last Modified: 2012-06-22
I have created by own thread object by deriving a class from TThread and overrided the Execute method with the code to execute.  As part of the thread code, I want to dynamically create a timer and fire a procedure I have written within the thread (the OnTimer event).  However, the OnTimer event never fires.  How to I get it to work?  This is the code I have written...

timMyTimer: TTimer; // This goes in private section of TMyThread class

This is how I've created the timer at the beginning of the execute method...

timMyTimer := TTimer.Create(Main);
timMyTimer.OnTimer := OnTimerEvent;
timMyTimer.Interval :=1000;
timMyTimer.Enabled := True;

procedure TMyThread.OnTimerEvent(Sender: TObject);
begin
  // This should fire every second, but doesn't!
end;

Thanks

Richard2000
0
Comment
Question by:Richard2000
  • 2
4 Comments
 
LVL 6

Expert Comment

by:DrDelphi
ID: 2747861
are you sure your thread is running?

0
 
LVL 3

Expert Comment

by:f15iaf
ID: 2748035
Ttimer class is a thread object and i don't sure if you can run thread objects one inside another.
0
 
LVL 6

Accepted Solution

by:
DrDelphi earned 50 total points
ID: 2748132
type
  TTimerThread=class(TThread)
  timer:TTimer;
  procedure Execute;override;
  Procedure TimerEvent(Sender:Tobject);
  end;



type
  TForm1 = class(TForm)
    Label1: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  timethread:TTimerThread;
implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Timethread:=TTimerThread.Create(false);
end;

{ TTimerThread }

procedure TTimerThread.Execute;
begin
   timer:=TTimer.Create(form1);
   Timer.interval:=1000;
   timer.ontimer:=TimerEvent;
   timer.enabled:=true;
   while not terminated do
   begin
     application.processmessages;
   end;
end;

procedure TTimerThread.TimerEvent(Sender: Tobject);
begin
   Form1.Label1.caption:=timeTostr(now);
end;

end.
0
 
LVL 3

Expert Comment

by:shenqw
ID: 2748162
unit Unit2;

interface

uses
  Classes,extctrls,Dialogs;

type
  TTest = class(TThread)
  private
    { Private declarations }
    Timer:TTimer;
    procedure TimerEvent(Sender:TObject);
  protected
    procedure Execute; override;
    procedure ShowOK;
  public
    constructor Create(CreateSuspended: Boolean);
    destructor  Destroy;override;
  end;

implementation

{ Test }

constructor TTest.Create(CreateSuspended: Boolean);
begin
  inherited;
  Timer:=TTimer.Create(nil);
  Timer.Interval:=1000;
  Timer.OnTimer:=TimerEvent;
end;

destructor TTest.Destroy;
begin
  Timer.Free;
  inherited;
end;

procedure TTest.Execute;
begin
  while Terminated do ;
end;

procedure TTest.ShowOK;
begin
  ShowMessage('Ok');
end;

procedure TTest.TimerEvent(Sender: TObject);
begin
  Timer.Enabled:=False;
  Synchronize(ShowOK);
  Timer.Enabled:=True;
end;

end.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 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