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

x
?
Solved

Using TTimer in Threads - OnTimer event never fires.

Posted on 2000-04-25
4
Medium Priority
?
1,135 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
[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
  • 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 200 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

Independent Software Vendors: 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!

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

604 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