I use Indy 9 with Delphi 5. In my application I want to communicate with a network device via UDP. So I use UDPServer comp. in a class which is derived from TThread. When I write similar to the following code then CPU usage is 100%.
in the thread :
while not terminated do begin
if GetMessage(Msg, 0, 0, 0) then begin
if Msg.message = WM_UDPMSG then
Break
else
DispatchMessage(Msg);
end;
end;
and OnUDPRead event :
try
// Processing the data here
except
PostThreadMessage(ThreadId, WM_UDPMSG, 0, 0);
end;
When I use Sleep function in the while-do loop or in OnUDPRead event there is no change. Still the CPU usage is 100%.
My thread priority is Normal.
How can I solve my problem?
The problem you have is because you're receiving the UDP data in the GUI thread, but want to process the data in another thread.
The real problem is that your trying to use a asynchronous component in a blocking way. The better solution would be to use a real blocking UDP communication library such as synapse. Then it's very easy to just wait for new data to receive in your thread.
You could just write:
while not Terminated do
begin
BytesRead := FSocker.RecvBufferEx(@(Buffer[0]), BufferSize, Timeout);
if (BytesRead = 0) then
begin
// continue or exit if the receiving Failed
case FSocket.LastError of
0, WSAETIMEDOUT: Continue;
WSAECONNRESET, WSAENETRESET,
WSAENOTCONN, WSAECONNABORTED,
WSAENETDOWN:
begin
CloseConnection;
Exit;
end;
else
CloseConnection;
Exit;
end;
end;
// process the data in the buffer
end;
I'm not intimate with delphi code, but you are running a busy-wait mechanism which is grinding your CPU.
Introducing a sleep or a delay to the loop only hides the problem. I suggest using a better method for receiving your messages/events. Many solutions exist, like the observer-listener pattern, or thread wait and notify schemes.
1 You need a version of Indy newer than 9.0.0.18, I think. Older ones have show-stopping threading bugs. That includes all versions of Indy delivered with Delphi up to version 7.
2 Take a look at the sample code on how to work with Indy.
http://www.indyproject.org/demos/index.html
Is there a version of GetMessage which waits (blocks the thread) until a message arrives?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With