gpt4 book ai didi

delphi服务应用程序在15秒后停止,计时器不执行

转载 作者:行者123 更新时间:2023-12-02 07:37:40 25 4
gpt4 key购买 nike

我想在 Delphi 中制作服务应用程序,每天下午 02:00 运行并复制一些文件。所以我用了定时器。但控制不会发生计时器事件并且服务会在 15 秒内终止。我写了一个关于定时器事件的代码。如何在服务中使用计时器?请帮忙。提前致谢。

我的代码在这里:

unit untMain;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.SvcMgr, Vcl.Dialogs, Vcl.ExtCtrls, DateUtils, Vcl.Forms,
untCommon;

type
TsrvBackupService = class(TService)
tmrCopy: TTimer;
procedure tmrCopyTimer(Sender: TObject);

private
strlstFiles : TStringList;
{ Private declarations }
public
{ Public declarations }
end;

var
srvBackupService: TsrvBackupService;

implementation

{$R *.DFM}

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
srvBackupService.Controller(CtrlCode);
end;


procedure TsrvBackupService.tmrCopyTimer(Sender: TObject);
var
strCurTime : string;
strBKPpath : string;
strBKPTime : string;
NowDay : word;
NowMonth : word;
NowYear : word;
NowHour : word;
NowMin : word;
NowSec : word;
NowMilli : Word;
begin
DecodeTime(now,NowHour,NowMin,NowSec,NowMilli);
strCurTime := IntToStr(NowHour)+':'+IntToStr(NowMin);
strBKPTime := '14:00'
strBKPpath := ExtractFilePath(Application.ExeName);
if strCurTime = strBKPTime then begin
Try
CopyFile(PChar('c:\datafile.doc'),PChar(strBKPpath + 'datafile.doc'),true);
except
on l_e: exception do begin
MessageDlg(l_E.Message,mtError,[mbOk],0);
end;
end;
end;
end;

end.

最佳答案

使用在 OnStart 事件中启动的简单线程代替计时器。

教程在这里:

http://www.tolderlund.eu/delphi/service/service.htm

TTimer 更适合 GUI 应用程序。他们需要一个消息泵(参见here):

TTimer requires a running message queue in order to receive the WM_TIMER message that allows the OS to pass the message to the HWND, or to trigger the specified callback

关于delphi服务应用程序在15秒后停止,计时器不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12437294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com