I am trying to send a message via a ZeroMQ PublisherSocket
but I can't seem to get it right. I have tried two different approaches but both have their own problems.
SendFrame method
PublisherSocket mainSendSocket = context.CreatePublisherSocket();
mainSendSocket.Connect(...);
Then later on, I would simply call
mainSendSocket?.SendFrame(...);
This is how I would expect things to work, but the problem is I have found that sometimes I get a signifigant delay between when my WinForms application button is pressed and the message is actually sent. I know this because I am controlling some custom hardware and I get feedback right away.
Poller and SendReady event
This method doesn't have any delay but it results in the application using 100% of one CPU core. (50% of a dual core, 25% of a quad core, etc.)
PublisherSocket mainSendSocket = context.CreatePublisherSocket();
mainSendSocket.Connect(...);
poller = new Poller();
poller.AddSocket(mainSendSocket);
mainSendSocket.SendReady += mainSendSocket_SendReady;
Then in my event handler function I would use a ConcurrentQueue to check if there are any messages to be sent and send them using the same SendFrame
method. When I want to send a message I would have to add the message to the queue so it is picked up by the event handler the next time it runs.
I know that the problem with the CPU usage is because the SendReady
event keeps getting run as long a message can be send even if there is nothing to do but the documentation is kinda lacking on this particular area.
I'm not sure what I can do here, ideally I would like to determine the cause of the delay incurred in the first scenario but a solution to the CPU usage in the second approach or even a better third approach would be welcome.