如何在多线程中控制时钟在间隔一段时间后触发一事件?(100分)

  • 主题发起人 主题发起人 lylyly
  • 开始时间 开始时间
利用TIMER构件就可以,不过要是在线程中动态创建,设置好间隔,编写事件就可以了
 
创建一个线程时钟就行了,具体用法和TTimer控件一样
 
有具体事例吗?我对线程不是很了解
 
foolaliu 这么没有把应了?不是出了什么问题
 
foolaliu 这么没有把应了?不是出了什么问题
 
有具体事例吗?我对线程不是很了解
对线成都不了解,那你怎么做呀
function createthead(
lptheadattributes: pointer;安全属性 可社为 nil
dwstacksize:dword;线程堆栈大小社为 0 和应用程序一样大小
lpstartaddres:tfntheadstartroutine;指定要调入的线程函数地址
lpparamter:pointer;给线程函数传递的参数
dwcreationflags:dword;与你的线程相关的标志一般为 0
var lpthreadid: dword;为 0 即可09
) Thandle;
 
呵呵,这几天上大富翁很慢,没来回答,下面是源码。
TThreadedTimer = class;
{ 线程时钟专用之时钟线程 }
TTimerThread = class(TThread)
OwnerTimer: TThreadedTimer;
procedure Execute;
override;
proceduredo
Timer;
end;

{
TThreadedTimer :线程时钟
缺省定时1秒种,创建后即运行,线程挂起,优先级普通
}
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;
property Enabled: Boolean read FEnabled write SetEnabled;
property Interval: Word read FInterval write SetInterval;
property OnTimer: TNotifyEvent read FOnTimer write SetOnTimer;
property ThreadPriority: TThreadPriority read FThreadPriority write SetThreadPriority;
end;


{-----------------以下为线程时钟说明----------------------}
{初始化,缺省定时1秒种,创建后即运行,线程挂起,优先级普通}
constructor TThreadedTimer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FEnabled := True;
FInterval := 1000;
FThreadPriority := tpNormal;
FTimerThread := TTimerThread.Create(False);
FTimerThread.OwnerTimer := Self;
FTimerThread.Suspend;
end;

{线程执行,进入休眠直至被特定内部时钟唤醒,对I/O回调无反应}
procedure TTimerThread.Execute;
begin
Priority := OwnerTimer.FThreadPriority;
repeat
SleepEx(OwnerTimer.FInterval, False);
Synchronize(DoTimer);
until Terminated;
end;

{同步线程时钟的Ontimer事件}
procedure TTimerThread.DoTimer;
begin
OwnerTimer.Timer;
end;

{控制线程是否挂起}
procedure TThreadedTimer.UpdateTimer;
begin
if not FTimerThread.Suspended 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;
UpdateTimer;
end;
end;

{对OnTimer事件赋值}
procedure TThreadedTimer.SetOnTimer(Value: TNotifyEvent);
begin
FOnTimer := Value;
UpdateTimer;
end;

{设置线程优先级}
procedure TThreadedTimer.SetThreadPriority(Value: TThreadPriority);
begin
if Value <> FThreadPriority then
begin
FThreadPriority := Value;
UpdateTimer;
end;
end;

{定时激发用户的外部过程}
procedure TThreadedTimer.Timer;
begin
if Assigned(FOnTimer) then
FOnTimer(Self);
end;

{资源释放}
destructor TThreadedTimer.Destroy;
begin
FEnabled := False;
UpdateTimer;
FTimerThread.Free;
inherited Destroy;
end;

这下可以了把,给分吧,呵呵
 
接受答案了.
 
foolaliu,什么是线程时钟,时钟线程?谁解释一下foolaliu写的源程序?多谢!
我的Email:ant_elep@263.net
 
后退
顶部