bestlong 怕失憶論壇
標題:
用 Delphi 作 服務 + 多執行緒 的參考程式
[打印本頁]
作者:
bestlong
時間:
2006-8-4 16:47
標題:
用 Delphi 作 服務 + 多執行緒 的參考程式
unit Unit_main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr,Dialogs,
DB, ADODB,iniFiles,Forms,Unit_Import;
type
TImportDataSVC = class(TService)
ADOConn: TADOConnection;
ADOStoredProc: TADOStoredProc;
procedure ServiceContinue(Sender: TService; var Continued: Boolean);
procedure ServiceExecute(Sender: TService);
procedure ServicePause(Sender: TService; var Paused: Boolean);
procedure ServiceShutdown(Sender: TService);
procedure ServiceStart(Sender: TService; var Started: Boolean);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
private
{ Private declarations }
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;
var
ImportDataSVC: TImportDataSVC;
ImportThread: TImport;
implementation
uses FunCrypt;
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
ImportDataSVC.Controller(CtrlCode);
end;
function TImportDataSVC.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TImportDataSVC.ServiceContinue(Sender: TService; var Continued: Boolean);
begin
while not Terminated do
begin
Sleep(10);
ServiceThread.ProcessRequests(False);
end;
end;
procedure TImportDataSVC.ServiceExecute(Sender: TService);
begin
while not Terminated do
begin
Sleep(10);
ServiceThread.ProcessRequests(False);
end;
end;
procedure TImportDataSVC.ServicePause(Sender: TService; var Paused: Boolean);
begin
Paused := True;
end;
procedure TImportDataSVC.ServiceShutdown(Sender: TService);
begin
Status := csStopped;
ReportStatus();
end;
procedure TImportDataSVC.ServiceStart(Sender: TService; var Started: Boolean);
var
MyIniFile: TInifile;
Pwd, DBConStr, MS_SQLSERVER, MS_SQLUsr, MS_SQLPwd: String;
OracleParaList: TStringList;
begin
//服務啟動時,取得資料庫參數
MyIniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Dbsetup.ini');
OracleParaList.Create;
MS_SQLSERVER := MyIniFile.ReadString('MS-SQL', 'Server', '');
MS_SQLUsr := MyIniFile.ReadString('MS-SQL', 'User', '');
MS_SQLPwd := MyIniFile.ReadString('MS-SQL', 'Password', '');
MS_SQLPwd := _Crypt(MS_SQLPwd, 65535, false);
DBConStr := 'Provider=SQLOLEDB.1;Persist Security Info=True;'
+ 'User ID=' + MS_SQLUsr + ';Initial Catalog=GWBNMember;Data Source='
+ MS_SQLSERVER + ';Password=' + MS_SQLPwd;
OracleParaList.Add(MyIniFile.ReadString('Oracle', 'Server', ''));
OracleParaList.Add(MyIniFile.ReadString('Oracle', 'User', ''));
Pwd := MyIniFile.ReadString('Oracle', 'Password', '');
OracleParaList.Add(_Crypt(Pwd, 65535, false)); //解密密码
ADOConn.ConnectionString := DBConStr;
ADOConn.Connected := True;
ImportThread := TImport.Create(false, ADOConn, OracleParaList);
Started := True;
end;
procedure TImportDataSVC.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
Stopped := True;
end;
end.
複製代碼
此處為執行緒程式
unit Unit_Import;
interface
uses
inifiles, Classes, Sysutils, DB, ADODB, Forms;
type
TImport = class(TThread)
private
FADOConn: TADOConnection;
FAdoStoredProc: TADOStoredProc;
{ Private declarations }
protected
procedure Execute; override;
public
constructor Create(Suspended:Boolean; ADOConn:TADOConnection; OracleParaList:TStringList);
end;
implementation
{ Import }
constructor TImport.Create(Suspended:Boolean; ADOConn:TADOConnection; OracleParaList:TStringList);
begin
inherited Create(Suspended);
FADOConn := ADOConn;
FAdoStoredProc := TAdoStoredProc.Create(nil);
FAdoStoredProc.Connection := FADOConn;
FreeOnTerminate := False;
end;
procedure TImport.Execute;
var
Hour, Min, Sec, MSec: Word;
TimeStamp: String;
MyIniFile: TIniFile;
begin
{ Place thread code here }
{每次資料匯入執行時間}
FreeOnTerminate := false;
MyIniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Options.ini');
TimeStamp := MyIniFile.ReadString('Option', 'ImportTime', '00:00:00');
while not Terminated do
begin
DecodeTime(Time, Hour, Min, Sec, MSec);
if Trim(TimeStamp) = Format('%-2.2d:%-2.2d', [Hour, Min]) then //如果到匯入數據時間
begin
{匯入資料的程式部分}
Sleep(60000);
end;
end;
MyIniFile.free;
end;
end.
複製代碼
歡迎光臨 bestlong 怕失憶論壇 (http://www.bestlong.idv.tw/)
Powered by Discuz! X1.5