【转】利用线程更新ListView (2014-09-28 08:25:20)

http://blog.sina.com.cn/s/blog_44fa172f0102v2x0.html

procedure TForm5.Button3Click(Sender: TObject);
begin
  TThread.CreateAnonymousThread(procedure ()
  var
    I: Integer;
    Total: Integer;
  begin
    Total := 0;
    for I := 1 to MaxValue do
    begin
      if (I * 10 mod MaxValue) = 0 then
        TThread.Synchronize (TThread.CurrentThread,
          procedure ()
          begin
            ListView1.Items.Add.Text := 'Th: ' + I.ToString;
          end);

      if IsPrime (I) then
        Inc (Total);
    end;

    TThread.Synchronize (TThread.CurrentThread,
      procedure ()
      begin
        ListView1.Items.Add.Text := 'Thread: ' + Total.ToString;
        NotifyComplete;
      end);
  end).Start;
end;

笔者测试,xe6可以编译通过,xe7无法编译通过。
    TThread.CreateAnonymousThread(
      procedure()
      begin
        TThread.Synchronize(TThread.CurrentThread,
          procedure()
          begin
            button1.Text := 'aaaa';
          end);
      end).Start;

10.8放假回来,xe7又可以编译上面的代码了!
10.10.反复的修改代码,发现无法编译的代码是在Synchronize中引用了局部循环变量,如果再声明一个临时变量来取代这个循环变量,就可以编译通过。

原文

http://www.dfwlt.com/forum.php?mod=viewthread&tid=711

原文地址:https://www.cnblogs.com/westsoft/p/8458775.html