简体   繁体   English

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

[英]Delphi code completion fail with anonymous methods

Please create a new FMX application, add a button and a memo to run this example. 请创建一个新的FMX应用程序,添加一个按钮和一个备忘录来运行此示例。 I have this code: 我有这个代码:

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;

It works as I expect but the problem is in the IDE. 它按预期工作,但问题出在IDE中。 If I place the cursor somewhere in the body of the anonymous function, I get the closing of the finally statement automatically. 如果我将光标放在匿名函数体内的某处,我会自动关闭finally语句。

How can I fix this? 我怎样才能解决这个问题?


First I am here 首先我在这里

在此输入图像描述

Then I press enter and I have this! 然后我按回车,我有这个!

在此输入图像描述

If you put the cursor at the beginning and not at the end of the line, you can add new spaces without the completion. 如果将光标放在行的开头而不是行的末尾,则可以添加新的空格而不完成。 How to solve this problem? 如何解决这个问题呢? Well, I have discovered that the issue happens because there is this code: 好吧,我发现问题发生是因为有这样的代码:

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

If you remove this code, the issue doens't happen anymore. 如果删除此代码,问题就不会再发生了。 Is this a bug in the IDE? 这是IDE中的错误吗?

Is this a bug in the IDE? 这是IDE中的错误吗?

Yes. 是。 This is a defect. 这是一个缺陷。 Please submit a report to Quality Portal. 请向Quality Portal提交报告。

Is this a bug in the IDE? 这是IDE中的错误吗?

Yes, this is a bug in the IDE. 是的,这是IDE中的一个错误。 Your code is syntactically valid. 您的代码在语法上是有效的。

How can I fix this? 我怎样才能解决这个问题?

The best way to avoid this is to create your code and surround it with try...except... to handle any exception: 避免这种情况的最好方法是创建代码并使用try...except...包围它try...except...来处理任何异常:

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

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

So your code will be: 所以你的代码将是:

  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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM