使用了多线程的计时器,是不是计时器的OnTime事件也属于多线程的?(50分)

  • 主题发起人 主题发起人 baoling
  • 开始时间 开始时间
B

baoling

Unregistered / Unconfirmed
GUEST, unregistred user!
使用了多线程的计时器,是不是计时器的OnTime事件也属于多线程的?
 
???不明白。。。[:(]
 
TTimer类是由系统生成,不能生成太多,好象数量是最多4个(我记不清了)
 
没有人用过这个控件吗?
以下是多线程计时器的源码:
//多线程
unit ThdTimer;
interface
uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, Dialogs;
type
TThreadedTimer = class;
TTimerThread = class(TThread)
OwnerTimer: TThreadedTimer;
Interval: DWord;
procedure Execute;
override;
proceduredo
Timer;
end;

TThreadedTimer = class(TComponent)
private
FEnabled: Boolean;
FInterval: Word;
FOnTimer: TNotifyEvent;
FTimerThread: TTimerThread;
FThreadPriority: TThreadPriority;
protected
procedure UpdateTimer;
procedure SetEnabled(Value: Boolean);
procedure SetInterval(Value: Word);
procedure SetOnTimer(Value: TNotifyEvent);
procedure SetThreadPriority(Value: TThreadPriority);
procedure Timer;
dynamic;
public
constructor Create(AOwner: TComponent);
override;
destructor Destroy;
override;
procedure Reset;
published
property Enabled: Boolean read FEnabled write SetEnabled default True;
property Interval: Word read FInterval write SetInterval default 1000;
property OnTimer: TNotifyEvent read FOnTimer write SetOnTimer;
property ThreadPriority: TThreadPriority read FThreadPriority
write SetThreadPriority;
end;

procedure Register;
implementation
procedure TTimerThread.Execute;
begin
repeat
SleepEx(Interval, False);
Synchronize(DoTimer);
until Terminated;
end;

procedure TTimerThread.DoTimer;
begin
OwnerTimer.Timer;
end;

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

destructor TThreadedTimer.Destroy;
begin
FEnabled := False;
UpdateTimer;
FTimerThread.Free;
inherited Destroy;
end;

procedure TThreadedTimer.UpdateTimer;
begin
if FTimerThread.Suspended = False then
FTimerThread.Suspend;

if (FInterval <> 0) and FEnabled and Assigned(FOnTimer) then
FTimerThread.Resume;
end;

procedure TThreadedTimer.SetEnabled(Value: Boolean);
begin
if Value <> FEnabled then
begin
FEnabled := Value;
UpdateTimer;
end;
end;

procedure TThreadedTimer.SetInterval(Value: Word);
begin
if Value <> FInterval then
begin
FInterval := Value;
FTimerThread.Interval := FInterval;
UpdateTimer;
end;
end;

procedure TThreadedTimer.SetOnTimer(Value: TNotifyEvent);
begin
FOnTimer := Value;
UpdateTimer;
end;

procedure TThreadedTimer.SetThreadPriority(Value: TThreadPriority);
begin
if Value <> FThreadPriority then
begin
FThreadPriority := Value;
FTimerThread.Priority := Value;
UpdateTimer;
end;
end;

procedure TThreadedTimer.Timer;
begin
if Assigned(FOnTimer) then
FOnTimer(Self);
//在这里放置的代码,是不是也属于多线程机制
end;

procedure TThreadedTimer.Reset;
begin
FTimerThread.Free;
FTimerThread := TTimerThread.Create(False);
FTimerThread.OwnerTimer := Self;
FTimerThread.Priority := FThreadPriority;
UpdateTimer;
end;

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

end.
 
因为现成没有自己的消息循环,所以ontimer应该是不分单多线程的
一家之言,欢迎批评指正
 
计时器的OnTime事件是不属于多线程的,是属于多任务
 
在说你的这个多线程的计时器之前,先说一下Delphi自带的计时器。Delphi自带的计时器
主要是通过发送WM_Timer消息触发的,这样做有一个缺点,就是当程序忙的时候消息机制
可能会有一段时间得不到控制权,这样就会阻塞WM_Timer,ontimer事件也就不能及时响应。
利用多线程的计时器可以解决这个缺点,但是你这个控件却不行,因为它利用
Synchronize(DoTimer)来响应ontimer事件,让我们看一下Synchronize的Code:
/////////////////////////////////////////////////////////
procedure TThread.Synchronize(Method: TThreadMethod);
begin
FSynchronizeException := nil;
FMethod := Method;
SendMessage(ThreadWindow, CM_EXECPROC, 0, Longint(Self));//关键代码
if Assigned(FSynchronizeException) then
raise FSynchronizeException;
end;
/////////////////////////////////////////////////////////////////////////
Synchronize的依然是通过向主线程发送消息来让主线程执行特定的过程,以此
达到同步的目的,这样非但没有解决上面的缺点,反而两个线程相互切换浪费系统
资源。解决的方法是抛弃Synchronize,但这样一来你要自己解决线程之间的资源共享
问题。
 
后退
顶部