Use something like this to initialize the queue:
string queuePath = GetInputQueueName();
if (!MessageQueue.Exists(queuePath))
{
inputMessageQueue = MessageQueue.Create(queuePath);
}
else
{
inputMessageQueue = new MessageQueue(queuePath);
}
inputMessageQueue.Formatter = new BinaryMessageFormatter();
inputMessageQueue.ReceiveCompleted += OnReceiveCompleted;
inputMessageQueue.BeginReceive();
Here is a simple implmentation of
OnReceiveCompleted
, remember
that this code is not executing in the UI thread, so use the
Control.InvokeRequired[
^] /
Control.Invoke[
^] pattern to interact with the UI thread.
private void OnReceiveCompleted(Object source,ReceiveCompletedEventArgs asyncResult)
{
try
{
MessageQueue mq = (MessageQueue)source;
if (mq != null)
{
try
{
Message message = null;
try
{
message = mq.EndReceive(asyncResult.AsyncResult);
}
catch (Exception ex)
{
if (isRunning)
{
LogException(ex, System.Reflection.MethodBase.GetCurrentMethod());
}
}
if (message != null)
{
QueuedInput queuedInput = message.Body as QueuedInput;
if (queuedInput != null)
{
}
}
}
finally
{
if (isRunning)
{
mq.BeginReceive();
}
}
}
return;
}
catch (Exception exc)
{
LogException(exc, System.Reflection.MethodBase.GetCurrentMethod());
}
}
If this code is too slow, then it's probably the deserialization that kills the performance - a complex object graph takes a significant amount of time to deserialize - but you may find an answer to that in this article:
Optimizing Serialization in .NET[
^]
I've used something quite similar to this to handle many times the load you are taking about.
Update
Microsoft Message Queuing – A simple multithreaded client and server[
^] - I hope you find that this solves your problems :)
Update 2
Microsoft Message Queuing – Log Trade information using Microsoft SQL Server[
^] - another take at describing how to solve your problems :)
Regards
Espen Harlinn