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: