Avatar of Actia
Actia

asked on 

TTimer created by TThread

I've got such a Thread:


  TReadRespThread = class(TThread)
  private
    { Private declarations }
    Timer : TTimer;
    procedure NewTimer1Timer(Sender: TObject);
  protected
    procedure Execute; override;
  public
  end;


procedure TReadRespThread.Execute;
begin
  inherited;
  Timer := TTimer.Create(Application);
  Timer.Interval := 100;
  Timer.OnTimer := NewTimer1Timer;
  Timer.Enabled := True;
end;

procedure TReadRespThread.NewTimer1Timer(Sender: TObject);
begin
  //for some reason the program flow never comes here

end;

1. Why doesn't it work?
2. Is such a Timer running in a separate thread (not in the main thread)?

Jackie boy  :-)
Delphi

Avatar of undefined
Last Comment
AdminPPN
SOLUTION
Avatar of Mike Littlewood
Mike Littlewood
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of robert_marquardt
robert_marquardt

A timer in a thread is ridiculous. What do you want to achieve?
Avatar of Actia
Actia

ASKER

A timer in a thread is ridiculous. What do you want to achieve?

*** The reason is:

I have a dll with some functions inside. I have also the demo application showing how to use them. In this demo application there is a TTimer with Interval := 100, OnTimer it triggers function to check the values of a variable populated by the functions from the dll.
Unfortunatelly there is no way to catch the moment where this variable is populated, that is why the TTimer checks it.

I have the old application which works fine with the old hardware. My task is to make it work with the new hadware (using the dll). The way it works now is:

it sends command to the hardware using WriteFile function
it receives the response using ReadFile

It would be great to replace these Windows commands with functions from DLL,
it would save a huge amount of work.

So my plan was to:
1. Send a command using dll function (it works fine)
2. Run TTimer in working thread to check if we have a response with 100 ms intervals
3. When we got then CreateEvent(MyEvent);
4. In main thread: WaitForSingleObject(MyEvent, 3000) to stop main thread until it gets the response to the command sent
(WaitForSingleObject blocks the main thread and this is why I can't use TTimer put on the Form).
Provide the old application with the response adjusted to the variable it works with now.

hope I explained it properly

Jackie boy
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Actia
Actia

ASKER

This is almost exactly what I implemented! :-)
And this is USB device :-)

Jackie boy
Avatar of D-Master
D-Master
Flag of Palestine, State of image

Hi,

you can use the JCL TThreadTimer Component from the the JCL Threading.

you may download it from here
http://sourceforge.net/project/showfiles.php?group_id=45786
Avatar of D-Master
D-Master
Flag of Palestine, State of image

sorry
Tell me more about the device directly (robert_marquardt att gmx dott de). I am always interested in USB devices.
Avatar of AdminPPN
AdminPPN
Flag of Australia image

JCl component for Threaded timer has a bug if you disable the time inside the ontimer function.
Delphi
Delphi

Delphi is the most powerful Object Pascal IDE and component library for cross-platform Native App Development with flexible Cloud services and broad IoT connectivity. It provides powerful VCL controls for Windows 10 and enables FMX development for Windows, Mac and Mobile. Delphi is your choice for ultrafast Enterprise Strong Development™. Look for increased memory for large projects, extended multi-monitor support, improved Object Inspector and much more. Delphi is 5x faster for development and deployment across multiple desktop, mobile, cloud and database platforms including 32-bit and 64-bit Windows 10.

60K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo