简体   繁体   English

如何在 TIdTCPServer OnExecute 中同步线程

[英]How to synchronize thread in TIdTCPServer OnExecute

Someone who knows Delhi Indy 10 Sockets ?有人知道德里 Indy 10 插槽吗? the component TIdTCPServer.组件 TIdTCPServer。 I have a service where instantiate multiple threads ( multithreading ) using TIdTCPServer to process requests from several clients ( tidTCPClient ) .我有一个服务,其中使用 TIdTCPServer 来实例化多个线程(多线程)来处理来自多个客户端 (tidTCPClient) 的请求。

It's all working, no problem , but in the service ( the server) I have a screen where update a log and a list of connected users and log what they are doing and view this log in the memo main screen.一切正常,没问题,但是在服务(服务器)中,我有一个屏幕,其中更新日志和已连接用户的列表,并记录他们正在做什么并在备忘录主屏幕中查看此日志。

It turns out that being Multithreaded must use the Syncronize to update the memo that is in thread main .事实证明,多线程必须使用同步来更新线程 main 中的备忘录。 So far so ok right?到目前为止一切正常吗?

The memo will be updated in the event idTCPServerExecute component , only that what I have just as event argument is ( AContext : TIdContext ) where I caught the instance of the thread running to her can call the syncronize ?备忘录将在事件 idTCPServerExecute 组件中更新,只有我作为事件参数的内容是 (AContext: TIdContext),我捕获了运行到她的线程的实例可以调用同步?

wanted to do something like:想做类似的事情:

AContext.thread.Syncronize ( LogMemo ); AContext.thread.Syncronize ( LogMemo ); // Will write in the memo on the main thread. // 将写在主线程的备忘录中。

But I searched and not found the thread object.但我搜索并没有找到线程对象。 Does anyone know where is this object?有谁知道这个对象在哪里?

or how I could update the screen in this event without being in competition with other threads ?或者我如何在不与其他线程竞争的情况下在此事件中更新屏幕?

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;

You can derive a class from Indy's TIdSync or TIdNotify class and override the TIdSync.DoSynchronize() or TIdNotify.DoNotify() method to do what you need.您可以从 Indy 的TIdSyncTIdNotify类派生一个类并覆盖TIdSync.DoSynchronize()TIdNotify.DoNotify()方法来执行您需要的操作。

Or, in recent Delphi versions, you can use the anonymous procedure version of the static TThread.Synchronize() or TThread.Queue() methods.或者,在最近的 Delphi 版本中,您可以使用静态TThread.Synchronize()TThread.Queue()方法的匿名过程版本。

Examples of both approaches have been posted many times before on Embarcadero's forum, Indy's forum, StackOverflow, etc. Search around.这两种方法的例子之前已经在 Embarcadero 的论坛、Indy 的论坛、StackOverflow 等上多次发布。搜索一下。

I have a similar issue with TIdTCPServer.OnConnect(AContext: TIdContext) In my event handler I need to manipulate a TVirtualStringTree in the main thread.我有一个与 TIdTCPServer.OnConnect(AContext: TIdContext) 类似的问题在我的事件处理程序中,我需要在主线程中操作 TVirtualStringTree。 I googled this article:我在谷歌上搜索了这篇文章:

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

Code for me, running Delphi Rio 10.3 Indy 10, is:对我来说,运行 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;

In a similar way can be managed the OnExecute event.可以用类似的方式管理 OnExecute 事件。

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

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