Page 1 of 1

thread in 2 procedures

PostPosted: April 22nd, 2005, 1:23 pm
by hacchan
Hi,
I want to count the running time of a procedure (e.g. the procedure button1.click) with the timer on timer1.timer procedure.
But there is a problem : I can't do those 2 procedures at the same time. Any help? Some could would be thankful.
Regards,

PostPosted: May 1st, 2005, 9:51 pm
by Radagast
Can't you simply add to the procedure Timer as local variable, enable it in first line, and disable and get what you need then?
I think code would look like this:
Code: Select all
procedure xxx;
begin
var Timer1: TTimer;
begin
Timer1:=TTimer.create(?);
Timer1.Interval:=1;
Timer1.Tag:=0;
Timer1.Enabled:=true;
...
Timer1.enabled:=false;
end;

procedure Timer1.OnTimer(?);
begin
inc(tag);
end;

Question mark means I don't remember what is there (if anything).
I think tag variable isn't used by component itself and you can put there whatever you want.
Simple ideas are the best, but I don't know whether it works or not. Did you try something like that? Or could you just put here your source code?
PS. I'm not responsible for any effect this code will have on your computer (including blowing up).

PostPosted: May 9th, 2005, 12:22 pm
by Kambiz
Why using a timer? :roll:

PostPosted: May 9th, 2005, 6:02 pm
by Radagast
Why not? If you delete Timer from that code, it won't do what it is supposed to. That was first idea I had and if you have better one: go ahead. But I think it should work even if it isn't optimal.

PostPosted: May 9th, 2005, 10:20 pm
by Kambiz
It doesn't work. The thread is busy and cannot dispatch timer message, therefore no event is generated.

Code: Select all
procedure Foo;
var
  TimeBegin, TimeEnd: DWORD
  ElapsedTime: DWORD;
begin
  TimeBegin := GetTickCount;
  // Do your task here
  TimeEnd := GetTickCount;
  ElapsedTime := (TimeEnd - TimeBegin); // in milliseconds
end;

PostPosted: May 10th, 2005, 7:28 am
by Radagast
Sounds good. I never know whether GetTickCount works and is named like this. I think that my idea would work in one thread application or if you used some other timer (TThdTimer?). But that is better.