为什么我在Delphi XE中收到此警告的任何想法:
[DCC警告] Form1.pas(250):W1036变量'$ frame'可能尚未初始化
procedure TForm1.Action1Execute(Sender: TObject);
var
Thread: TThread;
begin
...
Thread := TThread.CreateAnonymousThread(
procedure{Anonymos}()
procedure ShowLoading(const Show: Boolean);
begin /// <------------- WARNING IS GIVEN FOR THIS LINE (line number 250)
Thread.Synchronize(Thread,
procedure{Anonymous}()
begin
...
Button1.Enabled := not Show;
...
end
);
end;
var
i: Integer;
begin
ShowLoading(true);
try
Thread.Synchronize(Thread,
procedure{Anonymous}()
begin
... // some UI updates
end
Thread.Synchronize(Thread,
procedure{Anonymous}()
begin
... // some UI updates
end
);
finally
ShowLoading(false);
end;
end
).NameThread('Some Thread Name');
Thread.Start;
end;
我的代码中没有任何名称框架或$ frame。我甚至不确定带$符号的$ frame是一个有效的标识符。
对我来说就像编译魔术一样。
PS:当然现实生活xosw除了Form1,Button1,Action1名称之外还有。
答案 0 :(得分:3)
在XE2中,此编译器警告不存在。 无论如何,为了让你的方法对编译器更清楚一点,试试这个:
procedure TForm1.Action1Execute(Sender : TObject);
var
Thread: TThread;
begin
Thread := TThread.CreateAnonymousThread(
procedure{Anonymos}()
Type
TProcRef = reference to procedure (const Show: Boolean);
var
i: Integer;
ShowLoading : TProcRef;
begin
ShowLoading:=
procedure (const Show: Boolean)
begin
Thread.Synchronize(Thread,
procedure{Anonymous}()
begin
// Button1.Enabled := not Show;
end
);
end;
ShowLoading(true);
... // and so on.
更新: 我刚刚检查了XE,这确实修复了编译器警告。
我在问题评论中提供的参考资料解释了警告和框架对象。 所以我猜XE编译器对ShowLoading正在创建的框架对象感到困惑。并创建一个明确的引用修复了警告。
更新2 :
只显示我的测试供您参考:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
Windows,Classes, SysUtils;
procedure Action1Execute;
var
Thread: TThread;
begin
Thread := TThread.CreateAnonymousThread(
procedure{Anonymos}()
Type
TProcRef = reference to procedure (const Show: Boolean);
var
i: Integer;
ShowLoading : TProcRef;
begin
ShowLoading:=
procedure (const Show: Boolean)
begin
Thread.Synchronize(Thread,
procedure{Anonymous}()
begin
// Button1.Enabled := not Show;
end
);
end;
ShowLoading(true);
try
Thread.Synchronize( Thread,
procedure{Anonymous}()
begin
// some UI updates
end
);
Thread.Synchronize( Thread,
procedure{Anonymous}()
begin
// some UI updates
end
);
finally
ShowLoading(false);
end;
end
); //.NameThreadForDebugging('Some Thread Name');
Thread.Start;
end;
begin
try
{ TODO -oUser -cConsole Main : Insert code here }
Action1Execute;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.