简体   繁体   English

Delphi/Indy 多线程服务器

[英]Delphi/Indy multithreading Server

I am trying to turn my app multithreading.我正在尝试将我的应用程序变成多线程。 What I want to achieve is: - Receive command via TidHTTPServer - Execute local action (might involve using tidHTTP to send/receive data to other services) - return execution result to the original caller我想要实现的是: - 通过 TidHTTPServer 接收命令 - 执行本地操作(可能涉及使用 tidHTTP 向其他服务发送/接收数据) - 将执行结果返回给原始调用者

since I am pretty new to multi-threading I would like to know if my design-idea is correct因为我对多线程很陌生,所以我想知道我的设计理念是否正确

  1. TMsgHandler=Class(TThread) TMsgHandler=Class(TThread)
  2. in TidHTTPServer.OnCommandGet I create a new instance of TMsgHandler and pass ARequestInfo and AResponseInfo在 TidHTTPServer.OnCommandGet 我创建了一个新的 TMsgHandler 实例并传递 ARequestInfo 和 AResponseInfo
  3. TMsgHandler.Excecute interprest the data TMsgHandler.Excecute 解释数据
  4. Can TMsgHandler.Execeute use Objects (descendants of TidHTTP) in my Main to communicate with other services? TMsgHandler.Execeute 可以在我的 Main 中使用 Objects(TidHTTP 的后代)与其他服务进行通信吗?
  5. TMsgHandler sends answer through AResponseInfo and terminates. TMsgHandler 通过 AResponseInfo 发送应答并终止。

will this work?这会起作用吗?

This is not the correct design.这不是正确的设计。

THTTPServer is a multi-threaded component. THTTPServer是一个多线程组件。 Its OnCommand... events are fired in the context of worker threads that Indy creates for you.它的OnCommand...事件在 Indy 为您创建的工作线程的上下文中触发。

As such, you do not need to derive your TMsgHandler from TThread .因此,您不需要从TThread派生TMsgHandler Do your TIdHTTP directly in the context of the OnCommand... thread instead.直接在OnCommand...线程的上下文中执行TIdHTTP A response will not be sent back to the client until your event handler exits (unless you send one manually).在您的事件处理程序退出之前,不会将响应发送回客户端(除非您手动发送)。 However, you should not share a single TIdHTTP from the main thread (unless you absolute need to, in which case you would need to synchronize access to it).但是,您不应从主线程共享单个TIdHTTP (除非您绝对需要,在这种情况下您需要同步对它的访问)。 You should create a new TIdHTTP dynamically directly in your OnCommand... / TMsgHandler code as needed.您应该根据需要直接在OnCommand... / TMsgHandler代码中动态创建一个新的TIdHTTP

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

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