我有一段关于多线程的代码,我看不明白,希望大家帮帮忙,解读一下!(30分)

  • 主题发起人 主题发起人 handong
  • 开始时间 开始时间
H

handong

Unregistered / Unconfirmed
GUEST, unregistred user!
这个是关于TIMER的多线程问题的!
unit ThdTimer;
interface
uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, Dialogs;
type
TThreadedTimer = class;
TTimerThread = class(TThread)
OwnerTimer: TThreadedTimer;
procedure Execute;
override;
end;

TThreadedTimer = class(TComponent)
private
FEnabled: Boolean;
FInterval: DWord;
FOnTimer: TNotifyEvent;
FTimerThread: TTimerThread;
protected
procedure Timer;
dynamic;
public
constructor Create(AOwner: TComponent);
override;
destructor Destroy;
override;
published
property Enabled: Boolean
read FEnabled write FEnabled default True;
property Interval: DWord
read FInterval write FInterval default 1000;
property OnTimer: TNotifyEvent
read FOnTimer write FOnTimer;
end;

procedure Register;
implementation
procedure TTimerThread.Execute;
var
dwTick: DWord;
iTick: Integer;
begin
Priority := tpNormal;
//tpTimeCritical;
repeat
dwTick := GetTickCount();
//Synchronize(OwnerTimer.Timer);
OwnerTimer.Timer;
iTick := OwnerTimer.FInterval - (GetTickCount() - dwTick);
if iTick < 0 then
iTick := 0;
Sleep(iTick);
until Terminated;
end;

constructor TThreadedTimer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FEnabled := False;
FInterval := 1000;
FTimerThread := TTimerThread.Create(False);
FTimerThread.OwnerTimer := Self;
end;

destructor TThreadedTimer.Destroy;
begin
with FTimerThreaddo
begin
Terminate;
WaitFor;
Free;
end;
inherited Destroy;
end;

procedure TThreadedTimer.Timer;
begin
if FEnabled and Assigned(FOnTimer) then
FOnTimer(Self);
end;

procedure Register;
begin
RegisterComponents('Samples', [TThreadedTimer]);
end;

end.
 

Similar threads

后退
顶部