简体   繁体   English

IdHttpServer表单标题未更新

[英]IdHttpServer form caption not updating

I know i have posted a similar question before but i am not able to get it working I have this simple code : 我知道我之前发过一个类似的问题,但我无法让它工作我有这个简单的代码:

procedure TfrmMain.srvrConnect(AContext: TIdContext); //idhttpserver on connect event
var
  S,C : String;
begin
 repeat
  s := s + AContext.Connection.Socket.ReadChar;
 until AContext.Connection.Socket.InputBufferIsEmpty = True;
 frmMain.caption := S;
 Memo1.Lines.Add(S);
end;

The strings displays ok in the memo but the caption doesn't get updated 备忘录中的字符串显示正常,但标题不会更新

TIdHTTPServer is a multi-threaded component. TIdHTTPServer是一个多线程组件。 TIdContext runs in its own worker thread. TIdContext在其自己的工作线程中运行。 You cannot safely update the Form's Caption (or do anything else with the UI) from outside of the main thread. 您无法从主线程外部安全地更新表单的Caption (或使用UI执行任何其他操作)。 You need to synchronize with the main thread, such as with the TIdSync or TIdNotify class. 您需要与主线程同步,例如与TIdSyncTIdNotify类同步。

On a side note, calling ReadChar() in a loop is very inefficient, not to mention error-prone if you are using Delphi 2009+ since it cannot return data for surrogate pairs. 另外,在循环中调用ReadChar()是非常低效的,如果你使用Delphi 2009+,更不用说容易出错,因为它无法返回代理对的数据。

Use something more like this instead; 使用更像这样的东西;

type
  TDataNotify = class(TIdNotify)
  protected
    Data: String;
    procedure DoNotify; override;
  public
    constructor Create(const S: String);
    class procedure DataAvailable(const S: String);
  end;

constructor TDataNotify.Create(const S: String);
begin
  inherited Create;
  Data := S;
end;

procedure TDataNotify.DoNotify;
begin
  frmMain.Caption := Data; 
  frmMain.Memo1.Lines.Add(Data); 
end;

class procedure TDataNotify.DataAvailable(const S: String);
begin
  Create(S).Notify;
end;

procedure TfrmMain.srvrConnect(AContext: TIdContext); //idhttpserver on connect event 
var 
  S: String; 
begin 
  AContext.Connection.IOHandler.CheckForDataOnSource(IdTimeoutDefault);
  if not AContext.Connection.IOHandler.InputBufferIsEmpty then
  begin
    S := AContext.Connection.IOHandler.InputBufferAsString; 
    TDataNotify.DataAvailable(S); 
  end;
end; 

First, make sure you are writing to the right variable. 首先,确保您正在写入正确的变量。 Are you sure that frmMain is the form you want the caption do change? 你确定frmMain是你希望标题改变的形式吗?

Also, you could try: 此外,你可以尝试:

procedure TfrmMain.srvrConnect(AContext: TIdContext); //idhttpserver on connect event
var
  S,C : String;
begin
 repeat
  s := s + AContext.Connection.Socket.ReadChar;
 until AContext.Connection.Socket.InputBufferIsEmpty = True;
 oCaption := S;
 TThread.Synchronize(nil, Self.ChangeCaption);
end;

procedure TfrmMain.ChangeCaption;
begin
 Self.Caption := oCaption;
 Memo1.Lines.Add(oCaption);
end;

And finally, make sure that the first line on S is not a blank line, because the form's caption will not show strings that contains a line feed. 最后,确保S上的第一行不是空白行,因为表单的标题不会显示包含换行符的字符串。

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

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