问题
I have a Delphi 6 application that uses the ICS component suite to do socket communications. I have my own server socket VCL component that creates client TWSocket sockets when a new session becomes available. The client sockets I create do have the Multithreaded property set to TRUE, but all that does is changes the way the client socket handles socket messages to a manner that is safe from a background thread (non-main VCL thread). TWSocket does not spawn a thread to handle socket data traffic, which is what I need.
I need to have the receive calls occur off the main VCL thread, the main user interface thread, because the incoming data to the client socket is audio data that needs to be processed rapidly, in 50-100 milliseconds or less. In other words, one hiccup on the main VCL thread and the audio stream is disrupted. This is why I want to push the OnDataAvailable() event that fires whenever incoming data is available on to a high priority background thread. In other words, I want to force the message processing loop belonging to the client TWSocket object to a background thread.
I believe I can do this by creating the client socket via a background thread, but I'm hoping to avoid that since currently I use a VCL component I made that acts as a socket server. This is the entity that Accepts the incoming connection and spawns the client sockets. The socket server is created on the main VCL thread.
Therefore my question is, is there a (relatively) easy way to create the client sockets so they use an existing background thread to do their socket processing, especially the FD_RECV message handling? If not an existing background thread, then I will create one for each client socket I create, but I need to know how to make sure the new TWSocket object uses that background thread when it runs its message loop that processes socket messages, so how would I do that?
回答1:
For other ICS/TWSocket users out there, the solution is in the ICS ThrdSrv demonstration project that comes with the package. Take a close look at that project, especially its use of the ThreadAttach() and ThreadDetach() methods. That sample project shows how to create client sockets that have message pumps that run in the context of a worker thread.
来源:https://stackoverflow.com/questions/8352818/how-can-i-push-a-twsockets-ondataavailable-event-to-a-background-thread-in-my