当我们的程序需要借助windows的排程器定时调用执行时,执行完任务后应该能够自动关闭。例如:定期发送报表邮件,ERP系统升级等。
实现上述功能,分三步走:一、FormCreate中从ini文件中取变量,准备数据。二、FormShow中写 if (执行结果) then Close。三、执行结果 result:=false; 执行…… result:=true;
procedure Tfmuperp.FormCreate(Sender: TObject);
var
sIniFile:String;
myIni:TIniFile;
sUpdFile:String;
begin
//取得升级参数
sToPath:=ExtractFilePath(Application.ExeName);
sIniFile:=sToPath+'dbconn.ini';
myIni:=TIniFile.create(sIniFile);
sUpdFile:=myIni.ReadString('CONFIG','update','');
sFromPath:=ExtractFilePath(sUpdFile);
FreeAndNil(myIni);
end;
function Tfmuperp.updComplete;
var
hErp:Hwnd;
sfosCopy:TShFileOpStruct;
begin
Result:=false;
//关闭ERP
hErp:=FindWindow(nil,'jhERPsystem');
if hErp<>0 then Sendmessage(hErp,wm_close,0,0);
sleep(2000);
//COPY ERP
FillChar(sfosCopy,SizeOf(TshFileOpStruct),#0);
sfosCopy.Wnd:=Handle;
sfosCopy.wFunc:=FO_Copy;
sfosCopy.pFrom:=pChar(sFromPath+'*.xls'+#0+#0);
sfosCopy.pTo:=pChar(sToPath);
sfosCopy.fFlags:=FOF_SILENT or FOF_NOCONFIRMATION;
ShFileOperation(sfosCopy);
if hErp<>0 then Sendmessage(hErp,wm_close,0,0);
sfosCopy.pFrom:=pChar(sFromPath+'jhERP.exe'+#0+#0);
ShFileOperation(sfosCopy);
sfosCopy.pFrom:=pChar(sFromPath+'report\*.*'+#0+#0);
sfosCopy.pTo:=pChar(sToPath+'report\');
ShFileOperation(sfosCopy);
Result:=true;
end;
procedure Tfmuperp.FormActivate(Sender: TObject);
begin
Animate1.Active:=true;
if (length(sFromPath)<5) then
begin
showmessage('Update is Failed, Please check the dbconn.ini->UpdFile');
close;
end else
begin
if updComplete then close;
end;
end;相关代码如下: