Link to home
Start Free TrialLog in
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  :-)
SOLUTION
Avatar of Mike Littlewood
Mike Littlewood
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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

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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Actia

ASKER

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

Jackie boy
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
sorry
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.