【发布时间】:2022-11-19 13:00:51
【问题描述】:
H1 我用OnCloseQuery 写了一个小程序来保存Application.Terminate 之前的数据。我想知道在电源故障或计算机崩溃的情况下这是否足够。
type
TForm1 = class(TForm)
abs: TABSDatabase;
ABSTable1: TABSTable;
....
ABSTable6: TABSTable;
....
var
Form1: TForm1;
isBusy : Boolean;
....
procedure TForm1.CloseTables;
var
x : Integer;
dummy : TABSTable;
begin
for x:=0 to ComponentCount-1 do
begin
if Components[x] is TABSDataSet then
begin
if Components[x] is TABSTable then
begin
dummy := (Components[x] as TABSTable);
if ((dummy.Active = True) and ((dummy.state = dsEdit) or (dummy.State = dsInsert))) then
begin
dummy.Post;
dummy.Active := False;
end
else
if dummy. Active = True then dummy.Close;
end;
end;
end;
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
if abs.Connected = True then isBusy := True else isBusy := False;
end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if isBusy = True then
begin
CanClose := False;
CloseTables;
abs.Connected := False;
isBusy := False;
Application.Terminate;
end
else CanClose := True;
end;
先感谢您。
编辑
我按照 David Heffernan 的建议修改了我的代码。
procedure TForm1.CloseTables;
var
x : Integer;
dummy : TABSTable;
begin
for x:=0 to ComponentCount-1 do
begin
if Components[x] is TABSDataSet then
begin
if Components[x] is TABSTable then
begin
dummy := (Components[x] as TABSTable);
if ((dummy.Active) and ((dummy.state = dsEdit) or (dummy.State = dsInsert))) then
begin
dummy.Post;
dummy.Active := False;
end
else
if dummy.Active then dummy.Close;
end;
end;
end;
end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if abs.Connected then
begin
CanClose := False;
CloseTables;
abs.Connected := False;
Application.Terminate;
end
else CanClose := True;
end;
【问题讨论】:
-
如果您删除所有这些代码,您将不会获得更好的行为,这对我来说一点也不明显。顺便说一句,不要写
if foo = True then,写if foo then。也别写if foo then bar := True else bar := False,写bar := foo -
如果您的计算机断电或崩溃,则没有任何帮助。
-
另外
isBusy在这里是全局的,但应该是一个表单成员变量。这也可能没有必要。你不能直接测试abs.Connected吗? -
如果电源故障让您过于担心,请购买 ups
-
假设您的青蛙模拟器应用程序中有一个类 {{TFrog}}。然后你创建一万个这样的实例——也就是说,你创建一万只在屏幕上跳来跳去的青蛙。每只青蛙都有自己的体重、颜色和年龄:这些是实例变量,也就是说,{{TFrog}} 类的每个实例——每只青蛙——都有自己的此类变量。另一方面,您的应用程序可能有一个全球时间或天气变量——在您的整个应用程序中只有一个这样的变量,无论您有多少只青蛙。
标签: delphi