TTimer created by TThread

I've got such a Thread:

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

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

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


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

Jackie boy  :-)
Who is Participating?
I would implement a component for it.

It has a Write method which writes to the device and then starts a thread to implement a response and a failure event.
The thread is started and does *not* implement a standard "while not Terminated do" loop.
It implements a for I := 1 to 30 do loop. In each iteration it sleeps for 100 ms and then checks for the DLL data.
If it finds the data then it synchronizes the response event to the main thread and terminates.
If it does not find the data then it triggers the failure event and terminates also.

To serialize the access you should make the Write method fail if a thread is running.

BTW this sounds suspiciously like an USB device. Can you tell me more about the device?
This is taken from the delphi help file about threads, though I dont have experience with them, it does seem to say dont create the timer this way

>Do not use the properties and methods of other objects directly in the Execute method of a thread.  Instead, separate the use of other objects into a separate procedure call, and call that procedure by passing it as a parameter to the Synchronize method.

Not sure if this is of any help
A timer in a thread is ridiculous. What do you want to achieve?
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

ActiaAuthor Commented:
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
ActiaAuthor Commented:
This is almost exactly what I implemented! :-)
And this is USB device :-)

Jackie boy

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

you may download it from here
Tell me more about the device directly (robert_marquardt att gmx dott de). I am always interested in USB devices.
JCl component for Threaded timer has a bug if you disable the time inside the ontimer function.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.