Delphi:服务中的计时器停止了我的 FileReader
Delphi: Timer in Service Stops my FileReader
我正在编写一个具有计时器的服务,该计时器应每 60 秒检查一次线路是否有特定号码。这是定时器的代码:
procedure TConnectionChecker.Timer2Timer(Sender: TObject);
var
myFile: TextFile;
sLine: string;
fileOpenLog: TStreamWriter;
fileOpenLogName, fileOpenLogPathName: string;
begin
ExePath := TPath.GetDirectoryName(GetModuleName(HInstance));
filename:= 'restult.txt';
filePath:= TPath.Combine(exePath, 'OutputFile');
filePathName:= TPath.Combine(filePath, filename);
fileOpenLogName:= 'Log_fileOpen.txt';
serviceLogPath:= TPath.Combine(exePath, 'LogFiles');
fileOpenLogPathName:= TPath.Combine(serviceLogPath, fileOpenLogName);
fileOpenLog := TStreamWriter.Create(TFileStream.Create(fileOpenLogPathName, fmCreate or fmShareDenyWrite));
if not FileExists(filePathName) then
begin
fileOpenLog.WriteLine('File not found');
TServiceThread.Current.Terminate;
fileOpenLog.Free;
end;
fileOpenLog.WriteLine('File found');
try
AssignFile(myFile, filePathName);
Reset(myFile);
fileOpenLog.WriteLine('File opened');
while NOT eof(myFile) do
begin
PingWorkedAufrufe:= PingWorkedAufrufe + 1;
readln(myFile, sLine);
fileOpenLog.WriteLine('Read Line: ' + sLine);
checkIfPingWorked(sLine);
end;
fileOpenLog.WriteLine('EOF');
finally
CloseFile(myFile);
end;
fileOpenLog.Free;
end;
这里是我的程序代码:checkIfPingWorked:
procedure TConnectionChecker.checkIfPingWorked(ALine: String);
var
AValue, StartOfLineToFind: String;
checkIfPingWorkedLog: TStreamWriter;
checkIfPingWorkedLogName, checkIfPingWorkedLogPathName: string;
begin
ExePath := TPath.GetDirectoryName(GetModuleName(HInstance));
StartOfLineToFind:= ' Pakete: Gesendet =';
ip:= '...';
fileName:= 'restult.txt';
filepath:= TPath.Combine(exePath, 'OutputFile');
filepathname:= TPath.Combine(filepath, fileName);
serviceLogPath:= TPath.Combine(exePath, 'LogFiles');
checkIfPingWorkedLogName:= 'Log_checkIfPingWorked.txt';
checkIfPingWorkedLogPathName := TPath.Combine(serviceLogPath, checkIfPingWorkedLogName);
checkIfPingWorkedLog := TStreamWriter.Create(TFileStream.Create(checkIfPingWorkedLogPathName, fmCreate or fmShareDenyWrite));
checkIfPingWorkedLog.WriteLine('Zeilen werden überprüft');
if Pos(StartOfLineToFind, ALine) = 1 then
begin
AValue:= Copy(ALine, 39, 1);
if AValue = IntToStr(5) then
checkIfPingWorkedLog.WriteLine('Success')
else
begin
checkIfPingWorkedLog.WriteLine('Error');
//Writing E-Mail...
//Authentifizierung
IdSMTP1.AuthType := TIdSMTPAuthenticationType.satDefault;
//Benutzerdaten für Authentifizierung
IdSMTP1.Username := '...';
IdSMTP1.Password := '...';
//Server-Daten
IdSMTP1.Host := '...';
IdSMTP1.Port := ...;
IdSMTP1.Connect;
try
IdMessage1.From.Address := '';
IdMessage1.Recipients.EMailAddresses := '';
//IdMessage1.CCList.EMailAddresses := '';
//IdMessage1.BCCList.EMailAddresses := '';
IdMessage1.Subject := '--AUTOMATISCHE BENACHRICHTIGUNG--';
IdMessage1.Body.Text := 'Der PC mit der IP: ' + ip + ' konnte nicht mehr erreicht werden';
IdSMTP1.Send(IdMessage1);
finally
IdSMTP1.Disconnect;
end;
checkIfPingWorkedLog.WriteLine('Email sent');
TServiceThread.Current.Terminate;
end;
end;
checkIfPingWorkedLog.Free;
end;
但是我只能看前两行然后就停了
重要的是我最初把它写成一个普通的程序。而且效果很好。现在我将其转换为 Windows 服务。
问题是,我每次都创建新的日志文件。
现在我在服务创建上创建一个日志文件:
procedure TConnectionChecker.ServiceCreate(Sender: TObject);
var
logFilePathName, logFileName, exePath, logFilePath: String;
begin
ExePath := TPath.GetDirectoryName(GetModuleName(HInstance));
logFileName:= 'log_connectionTest.txt';
logFilePath:= TPath.Combine(exePath, 'logFile');
logFilePathName:= TPath.Combine(logFilePath, logFileName);
if not TDirectory.Exists(logFilePath) then
TDirectory.CreateDirectory(logFilePath);
swLogFile:= TStreamWriter.Create(TFileStream.Create(logFilePathName, fmCreate or fmShareDenyWrite));
end;
我做了一个程序,我要求 2 个参数(目标和文本)并将行写入日志文件。:
procedure TConnectionChecker.WriteToLog(destination, Text: string);
begin
swLogFile.WriteLine('[' + DateTimeToStr(now) + '] ' + destination + ' schreibt: ' + text);
end;
终于可以在每个过程中调用它了:
WriteToLog('checkIfPingWorked', 'Success')
我正在编写一个具有计时器的服务,该计时器应每 60 秒检查一次线路是否有特定号码。这是定时器的代码:
procedure TConnectionChecker.Timer2Timer(Sender: TObject);
var
myFile: TextFile;
sLine: string;
fileOpenLog: TStreamWriter;
fileOpenLogName, fileOpenLogPathName: string;
begin
ExePath := TPath.GetDirectoryName(GetModuleName(HInstance));
filename:= 'restult.txt';
filePath:= TPath.Combine(exePath, 'OutputFile');
filePathName:= TPath.Combine(filePath, filename);
fileOpenLogName:= 'Log_fileOpen.txt';
serviceLogPath:= TPath.Combine(exePath, 'LogFiles');
fileOpenLogPathName:= TPath.Combine(serviceLogPath, fileOpenLogName);
fileOpenLog := TStreamWriter.Create(TFileStream.Create(fileOpenLogPathName, fmCreate or fmShareDenyWrite));
if not FileExists(filePathName) then
begin
fileOpenLog.WriteLine('File not found');
TServiceThread.Current.Terminate;
fileOpenLog.Free;
end;
fileOpenLog.WriteLine('File found');
try
AssignFile(myFile, filePathName);
Reset(myFile);
fileOpenLog.WriteLine('File opened');
while NOT eof(myFile) do
begin
PingWorkedAufrufe:= PingWorkedAufrufe + 1;
readln(myFile, sLine);
fileOpenLog.WriteLine('Read Line: ' + sLine);
checkIfPingWorked(sLine);
end;
fileOpenLog.WriteLine('EOF');
finally
CloseFile(myFile);
end;
fileOpenLog.Free;
end;
这里是我的程序代码:checkIfPingWorked:
procedure TConnectionChecker.checkIfPingWorked(ALine: String);
var
AValue, StartOfLineToFind: String;
checkIfPingWorkedLog: TStreamWriter;
checkIfPingWorkedLogName, checkIfPingWorkedLogPathName: string;
begin
ExePath := TPath.GetDirectoryName(GetModuleName(HInstance));
StartOfLineToFind:= ' Pakete: Gesendet =';
ip:= '...';
fileName:= 'restult.txt';
filepath:= TPath.Combine(exePath, 'OutputFile');
filepathname:= TPath.Combine(filepath, fileName);
serviceLogPath:= TPath.Combine(exePath, 'LogFiles');
checkIfPingWorkedLogName:= 'Log_checkIfPingWorked.txt';
checkIfPingWorkedLogPathName := TPath.Combine(serviceLogPath, checkIfPingWorkedLogName);
checkIfPingWorkedLog := TStreamWriter.Create(TFileStream.Create(checkIfPingWorkedLogPathName, fmCreate or fmShareDenyWrite));
checkIfPingWorkedLog.WriteLine('Zeilen werden überprüft');
if Pos(StartOfLineToFind, ALine) = 1 then
begin
AValue:= Copy(ALine, 39, 1);
if AValue = IntToStr(5) then
checkIfPingWorkedLog.WriteLine('Success')
else
begin
checkIfPingWorkedLog.WriteLine('Error');
//Writing E-Mail...
//Authentifizierung
IdSMTP1.AuthType := TIdSMTPAuthenticationType.satDefault;
//Benutzerdaten für Authentifizierung
IdSMTP1.Username := '...';
IdSMTP1.Password := '...';
//Server-Daten
IdSMTP1.Host := '...';
IdSMTP1.Port := ...;
IdSMTP1.Connect;
try
IdMessage1.From.Address := '';
IdMessage1.Recipients.EMailAddresses := '';
//IdMessage1.CCList.EMailAddresses := '';
//IdMessage1.BCCList.EMailAddresses := '';
IdMessage1.Subject := '--AUTOMATISCHE BENACHRICHTIGUNG--';
IdMessage1.Body.Text := 'Der PC mit der IP: ' + ip + ' konnte nicht mehr erreicht werden';
IdSMTP1.Send(IdMessage1);
finally
IdSMTP1.Disconnect;
end;
checkIfPingWorkedLog.WriteLine('Email sent');
TServiceThread.Current.Terminate;
end;
end;
checkIfPingWorkedLog.Free;
end;
但是我只能看前两行然后就停了
重要的是我最初把它写成一个普通的程序。而且效果很好。现在我将其转换为 Windows 服务。
问题是,我每次都创建新的日志文件。 现在我在服务创建上创建一个日志文件:
procedure TConnectionChecker.ServiceCreate(Sender: TObject);
var
logFilePathName, logFileName, exePath, logFilePath: String;
begin
ExePath := TPath.GetDirectoryName(GetModuleName(HInstance));
logFileName:= 'log_connectionTest.txt';
logFilePath:= TPath.Combine(exePath, 'logFile');
logFilePathName:= TPath.Combine(logFilePath, logFileName);
if not TDirectory.Exists(logFilePath) then
TDirectory.CreateDirectory(logFilePath);
swLogFile:= TStreamWriter.Create(TFileStream.Create(logFilePathName, fmCreate or fmShareDenyWrite));
end;
我做了一个程序,我要求 2 个参数(目标和文本)并将行写入日志文件。:
procedure TConnectionChecker.WriteToLog(destination, Text: string);
begin
swLogFile.WriteLine('[' + DateTimeToStr(now) + '] ' + destination + ' schreibt: ' + text);
end;
终于可以在每个过程中调用它了:
WriteToLog('checkIfPingWorked', 'Success')