How can I reduce CPU usage in a program using Anonymous pipes in a background worker?

I am writing a program that uses anonymous channels to communicate between different processes in our software package. We have the main executable file (we will call it “Front End”), which opens the individual executable files (“Modules”), and the program that I write will mainly act as an intermediary between all the various modules, sending serialized messages from one to another to perform various actions based on the sent message.

I currently have 2 background employees who are constantly listening to messages sent from Front End (to open a module) or Module (to send a message to another module)

private void bwModuleListener_DoWork(object sender, DoWorkEventArgs e) { ProcessMessage Message = null; AnonymousPipeServerStream ModuleReceiver = (AnonymousPipeServerStream)e.Argument; BinaryFormatter bf = new BinaryFormatter(); while (true) { Message = (ProcessMessage)bf.Deserialize(ModuleReceiver); if (Message != null) { bwModuleListener.ReportProgress(0, Message); Message = null; } } } private void bwFrontEndListener_DoWork(object sender, DoWorkEventArgs e) { ProcessMessage Message = null; AnonymousPipeClientStream FrontEndReceiver = (AnonymousPipeClientStream)e.Argument; BinaryFormatter bf = new BinaryFormatter(); while (true) { Message = (ProcessMessage)bf.Deserialize(FrontEndReceiver); if (Message != null) { bwFrontEndListener.ReportProgress(0, Message); Message = null; } } } 

The problem is that the background workers occupy a fairly significant amount of the processor (constantly 15% on my 3.4 GHz quad-core machine). I tried to hide the thread in order to slow down the loop iterations, but the loops actually hang in the bf.Deserialize () call until the called bf.Serialize () is called from the other end of the channel. Anyone have any suggestions on how I can make this more efficient?

+4
source share
1 answer

"loops are actually looping on bf.Deserialize" you mean they are locked. This is normal since Deserialize will not complete until it receives the necessary data from AnonymousPipeServerStream. Therefore, it should not consume the processor cycle.

+3
source

All Articles