在多线程的一个方法中要引入一个计时器(Timer),代码产生!每过60秒某个值(const)减一(30分)

  • 主题发起人 主题发起人 小邱
  • 开始时间 开始时间

小邱

Unregistered / Unconfirmed
GUEST, unregistred user!
简单了!
 
给你一个threadtimer的代码,是别人写的,你可以参考一下,应该很快就可以解决。
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 := False;
FInterval := 1000;
FThreadPriority := tpHighest;//tpHigher;//tpNormal;//tpLowest;// tpLower;
FTimerThread := TTimerThread.Create(False);
FTimerThread.OwnerTimer := Self;
FTimerThread.Interval := FInterval;
FTimerThread.Priority := FThreadPriority;
FTimerThread.FreeOnTerminate := True;
end;

destructor TThreadedTimer.Destroy;
begin
FEnabled := False;
UpdateTimer;
FTimerThread.Terminate;
//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;
 
顶顶。学习上面代码先
 
unit U_TelRegThreed;
interface
uses
Classes,ExtCtrls,Dialogs,SysUtils,U_TelReg;
type
TConnect = class(TThread)
private
{ Private declarations }
fHosId,fDocId,fUserId:string;
fBalance,fTime,fRate:double;
//i:integer;
fTimer:TTimer;
procedure Timers(Sender:TObject);
procedure AccountTime();
protected
procedure Execute;
override;
public
constructor Create(Suspended:Boolean;HosId,DocId,UserId:string;Balance,Time,Rate:double);
end;
var
i:integer=0;
implementation
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TConnect.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ TConnect }
procedure TConnect.Timers(Sender:TObject);
begin
i:=i+1;
FTelReg.SB.Panels[0].Text :='预计:'+floattostr(fTime)+' 分钟';
FTelReg.SB.Panels[1].Text :='开始时间:'+Timetostr(Time);
FTelReg.SB.Panels[2].Text :='计时:'+floattostr(i)+' 分钟';
FTelReg.SB.Panels[3].Text :='状态:通话中……';
end;
procedure TConnect.AccountTime();
begin
fTimer:=TTimer.create(nil);
fTimer.Interval :=1000;
fTimer.OnTimer :=Timers;
end;
constructor TConnect.Create(Suspended:Boolean;HosId,DocId,UserId:string;Balance,Time,Rate:double);
begin
inherited Create(Suspended);
fHosId:=HosId;
fDocId:=DocId;
fUserId:=UserId;
fBalance:=Balance;
fTime:=Time;
fRate:=Rate;
FreeOnTerminate:=True;
end;
procedure TConnect.Execute;
begin
{ Place thread code here }
AccountTime();
end;

procedure TFTelReg.BitBtn1Click(Sender: TObject);
begin
inherited;
TConnect.Create(false,'102','0301','3518600029',156,48.75,3.2);
end;

为甚不执行procedure TConnect.Timers(Sender:TObject);???
 
接受答案了.
 
后退
顶部