Delphi代码完成失败,使用匿名方法

请创建一个新的FMX应用程序,添加一个按钮和一个备忘录来运行此示例.我有这个代码
procedure TForm1.Button1Click(Sender: TObject);
begin
  TTask.Run(procedure
            var
              client: TIdHTTP;
              result: string;
            begin
              client := TIdHTTP.Create(nil);
              try
                try
                  client.ReadTimeout := 4000;
                  client.ConnectTimeout := 4000;
                  result := client.Get('a valid url here just as test');
                  TThread.Synchronize(nil,procedure
                                           begin
                                             Memo1.Lines.Add(result);
                                           end);
                except
                  on E: Exception do
                    begin
                      TThread.Synchronize(nil,procedure
                                           begin
                                             Memo1.Lines.Add(E.Message);
                                           end);
                    end
                end;
              finally
                client.Free;
              end;
            end);
end;

它按预期工作,但问题出在IDE中.如果我将光标放在匿名函数体内的某处,我会自动关闭finally语句.

我怎样才能解决这个问题?

首先我在这里

然后我按回车,我有这个!

如果将光标放在行的开头而不是行的末尾,则可以添加新的空格而不完成.如何解决这个问题呢?好吧,我发现问题发生是因为有这样的代码

TThread.Synchronize(nil,procedure
                         begin
                           Memo1.Lines.Add(result);
                         end);

如果删除代码,问题就不会再发生了.这是IDE中的错误吗?

解决方法

Is this a bug in the IDE?

是的,这是IDE中的一个错误.您的代码在语法上是有效的.

How can I fix this?

避免这种情况的最好方法是创建代码并使用try …包围它…除了…来处理任何异常:

try
    MyClass := TComponent.Create(Self);
    try

    finally
      MyClass.Free;
    end;
  except on E: Exception do
  end;

所以你的代码将是:

TTask.Run(procedure
            var
              client: TIdHTTP;
              result: string;
            begin
              try
                Client := TIdHTTP.Create(nil);
                try
                  client.ReadTimeout := 4000;
                  client.ConnectTimeout := 4000;
                  result := client.Get('a valid url here just as test');
                  TThread.Synchronize(nil,procedure
                                           begin
                                             Memo1.Lines.Add(result);
                                           end);
                finally
                  Client.Free;
                end;
              except on E: Exception do
                begin
                  TThread.Synchronize(nil,procedure
                                           begin
                                             Memo1.Lines.Add(E.Message);
                                           end);
                  end;
              end;
            end;

相关文章

 从网上看到《Delphi API HOOK完全说明》这篇文章,基本上都...
  从网上看到《Delphi API HOOK完全说明》这篇文章,基本上...
ffmpeg 是一套强大的开源的多媒体库 一般都是用 c/c+&#x...
32位CPU所含有的寄存器有:4个数据寄存器(EAX、EBX、ECX和ED...
1 mov dst, src dst是目的操作数,src是源操作数,指令实现的...