thread in 2 procedures

Please discuss general Delphi programming topics here.

thread in 2 procedures

Postby hacchan » April 22nd, 2005, 1:23 pm

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,
hacchan
Active Member
Active Member
 
Posts: 6
Joined: December 8th, 2004, 3:30 am
Location: indonesia

Postby Radagast » May 1st, 2005, 9:51 pm

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).
Radagast
Active Member
Active Member
 
Posts: 24
Joined: May 1st, 2005, 9:32 pm
Location: Poland

Postby Kambiz » May 9th, 2005, 12:22 pm

Why using a timer? :roll:
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Postby Radagast » May 9th, 2005, 6:02 pm

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.
Radagast
Active Member
Active Member
 
Posts: 24
Joined: May 1st, 2005, 9:32 pm
Location: Poland

Postby Kambiz » May 9th, 2005, 10:20 pm

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;
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Postby Radagast » May 10th, 2005, 7:28 am

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.
Radagast
Active Member
Active Member
 
Posts: 24
Joined: May 1st, 2005, 9:32 pm
Location: Poland


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 20 guests

cron