繁体   English   中英

如何在 TIdTCPServer OnExecute 中同步线程

[英]How to synchronize thread in TIdTCPServer OnExecute

有人知道德里 Indy 10 插槽吗? 组件 TIdTCPServer。 我有一个服务,其中使用 TIdTCPServer 来实例化多个线程(多线程)来处理来自多个客户端 (tidTCPClient) 的请求。

一切正常,没问题,但是在服务(服务器)中,我有一个屏幕,其中更新日志和已连接用户的列表,并记录他们正在做什么并在备忘录主屏幕中查看此日志。

事实证明,多线程必须使用同步来更新线程 main 中的备忘录。 到目前为止一切正常吗?

备忘录将在事件 idTCPServerExecute 组件中更新,只有我作为事件参数的内容是 (AContext: TIdContext),我捕获了运行到她的线程的实例可以调用同步?

想做类似的事情:

AContext.thread.Syncronize ( LogMemo ); // 将写在主线程的备忘录中。

但我搜索并没有找到线程对象。 有谁知道这个对象在哪里?

或者我如何在不与其他线程竞争的情况下在此事件中更新屏幕?

procedure TfrmMainServer.TCPServerExecute(AContext: TIdContext);
var
     Cmd : String;
begin
     if AContext.Connection.IOHandler.Connected then
     begin
       If not AContext.Connection.IOHandler.InputBufferIsEmpty Then Begin
          Cmd  := AContext.Connection.IOHandler.ReadLn;

    // This is not acceptable in this way :

           memoFile.Lines.Add ('TESTING');
           txtInfoLabel.Caption := 'Arquivo enviado';
       End;
     end;
end;

您可以从 Indy 的TIdSyncTIdNotify类派生一个类并覆盖TIdSync.DoSynchronize()TIdNotify.DoNotify()方法来执行您需要的操作。

或者,在最近的 Delphi 版本中,您可以使用静态TThread.Synchronize()TThread.Queue()方法的匿名过程版本。

这两种方法的例子之前已经在 Embarcadero 的论坛、Indy 的论坛、StackOverflow 等上多次发布。搜索一下。

我有一个与 TIdTCPServer.OnConnect(AContext: TIdContext) 类似的问题在我的事件处理程序中,我需要在主线程中操作 TVirtualStringTree。 我在谷歌上搜索了这篇文章:

http://www.delphigroups.info/2/4/211251.html

对我来说,运行 Delphi Rio 10.3 Indy 10 的代码是:

uses IdSync;

procedure TMyClass.OnConnectFromClientChangedProc;
begin
  // Code to be executed in main thread
end;

procedure TMyClass.OnMyClientConnect(AContext: TIdContext);
begin
  TIdSync.SynchronizeMethod(OnConnectFromClientChangedProc);
end;

可以用类似的方式管理 OnExecute 事件。

暂无
暂无

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

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