How can I use TIdUDPServer in a console application?
The Indy UDP server component seems to depend on Windows messages, as I see new messages appear only when the mouse cursor moves over the form which contains the server component.
I created the component at run time using this code:
private
{ Private declarations }
Srv: TIdUDPServer;
...
procedure TForm1.FormCreate(Sender: TObject);
begin
Srv := TIdUDPServer.Create(Self);
Srv.DefaultPort := 9009;
Srv.OnUDPRead := Self.udpServerUDPRead;
Srv.Active := True;
end;
I would prefer a console or service application. Do I need an (invisible) window handle + some windows message queue code which to get this component working?
Update: some (inofficial) documentation says:
TIdUDPServer when active creates a listening thread to listen for inbound UDP packets. ... When ThreadedEvent is false, the OnUDPRead event will be fired in the context of the main program thread. When ThreadedEvent is true, the OnUDPRead event is fired in the context of the 开发者_运维问答listener thread.
If you use threading then you don't need Windows messages nor forms. Just set ThreadedEvent to true and handle in a thread, remembering that the event is coming in on a different thread so you will have to handle thread "synchronisation". I do this by only using threads that process messages off their own internal queue (my messages and my queue, not Windows messages or queues), so the event handler puts a message with the received data onto the thread's queue, to be processed in the context of the "destination" thread, not the Indy listener thread.
精彩评论