-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpingthreadunit.pas
59 lines (50 loc) · 1.35 KB
/
pingthreadunit.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
unit PingThreadUnit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, SQLDB, Forms;
type
TPingThread = class(TThread)
private
FConnection: TSQLConnection;
FPingInterval: Integer; // Ping interval in milliseconds
protected
procedure Execute; override;
public
constructor Create(AConnection: TSQLConnection; APingInterval: Integer);
end;
implementation
constructor TPingThread.Create(AConnection: TSQLConnection; APingInterval: Integer);
begin
inherited Create(True); // Create suspended
if Assigned(AConnection) then
FConnection := AConnection
else
raise Exception.Create('Invalid SQL Connection');
FPingInterval := APingInterval;
FreeOnTerminate := True; // Free thread automatically when it finishes
end;
procedure TPingThread.Execute;
begin
while not Terminated do
begin
try
if Assigned(FConnection) then
begin
// Perform a lightweight query to keep the connection alive
FConnection.ExecuteDirect('SELECT 1');
end
else
raise Exception.Create('SQL Connection is not assigned');
except
on E: Exception do
begin
// Handle the exception, e.g., log error or try reconnecting
// LogError(E.Message); // Implement logging if needed
end;
end;
// Sleep for the defined ping interval
Sleep(FPingInterval);
end;
end;
end.