Topic: Close program at a specific time - code snippets
I've hit a problem where users are forgetting to close the program when they leave work. This is partly due to the fact the IT department forbid PC's from being shutdown and must only go to screenlock.
Here is my code for people who would like to fiddle or use it as is in their own program. There is a time span during which the program will automatically close. This is to allow for some flexibility in the event someone stays later or doesn't finish on time.
It also allows for someone staying back to still use the program if they forget to untick the timepickers, it will just close during the timespan but not afterwards.
The call to the procedures are run in a timer called every 20 seconds at the moment. The reference to 'ClearLogs' is a procedure that deletes the user from the database tables user log entry.
The auto close code:
procedure AutoCloseSet;
var
TmeCompare : string;
begin {if timedate pickers are unchecked, this code is ignored}
if (frmAutoCloseSet.dtCloseSpanBegin.Checked) or (frmAutoCloseSet.dtCloseSpanEnd.Checked) then
begin
TmeCompare := TimeToStr(Time); //Time now
if (TmeCompare > TimeToStr(frmAutoCloseSet.dtCloseSpanBegin.DateTime)) and (TmeCompare < TimeToStr(frmAutoCloseSet.dtCloseSpanEnd.DateTime)) then
begin
ClearLogs; //delete user from user logs table
frmMain.Close; //close the program
end;
end;
end;
The set up form has two date time pickers where the user can choose their own time span for auto close or untick the option all together.
This form has:
procedure frmAutoCloseSet_OnShow (Sender: TObject; Action: string);
begin {Add one hour onto begin time span when form first opens. This is to prevent the possibility of Autoclosing before time has beenset}
frmAutoCloseSet.dtCloseSpanBegin.DateTime := frmAutoCloseSet.dtCloseSpanBegin.DateTime + EncodeTime(1,0,0,0);
end;
The timer code:
procedure OnTimer;
begin
iSeconds := iSeconds + 1;
AutocloseSet;
if iSeconds > 30 then
begin
// other code here requiring timer
iSeconds := 0;
end;
end;
Timer set up in main form onshow:
Timer := TTimer.Create (frmMain);
Timer.Interval := 20000;
Timer.Enabled := True;
Timer.OnTimer := @OnTimer;
Hope that might be useful for someone.