Polling on Mutliple MSMQs

V

Vai2000

Hi All, I have to poll on Multiple Queues for any inbound messages. I was
doing it fine earlier when there was only 1 Queue. I had added the
ReceiveCompletedEventHandler on the Queue and called the BeginReceive
method...
With multiple Queue's its a challenge...
I earlier implemented a logic provided by one of the forum users in which it
said to create an IAsyncResult Handle and then WaitHandle.WaitAny(array)...
but the Problem is when a new message arrives it doesn't notifies me....it
doesn't work the same as the EventHandler...

Here's the snippet I am using for Multiple Queues..its tied inside a WinSvc

protected override void OnStart(string[] args)
{

ThreadPool.QueueUserWorkItem(new WaitCallback(Execute));

}

void Execute(object key)
{

while(true)
{
aresMq1= mq1.BeginReceive();
aresMq2= mq2.BeginReceive();
Process();
}

}

void Process()
{

int iWaitIndex = WaitHandle.WaitAny(new
WaitHandle[]{aresMq1.AsyncWaitHandle, aresMq2.AsyncWaitHandle});

switch (iWaitIndex)
{

case 0:
Message msg1 = mq1.EndReceive(aresMq1);
Stream s1=PrepareMsg(msg1,mq1);
writeMessage(s1,msg1.Label);
break;

case 1:
Message msg2 = mq2.EndReceive(aresMq2);
Stream s2=PrepareMsg(msg2,mq2);
writeMessageEx(s2,msg2.Label);
break;

}
}
 
W

Willy Denoyette [MVP]

| Hi All, I have to poll on Multiple Queues for any inbound messages. I was
| doing it fine earlier when there was only 1 Queue. I had added the
| ReceiveCompletedEventHandler on the Queue and called the BeginReceive
| method...
| With multiple Queue's its a challenge...
| I earlier implemented a logic provided by one of the forum users in which
it
| said to create an IAsyncResult Handle and then
WaitHandle.WaitAny(array)...
| but the Problem is when a new message arrives it doesn't notifies me....it
| doesn't work the same as the EventHandler...
|
| Here's the snippet I am using for Multiple Queues..its tied inside a
WinSvc
|
| protected override void OnStart(string[] args)
| {
|
| ThreadPool.QueueUserWorkItem(new WaitCallback(Execute));
|
| }
|
| void Execute(object key)
| {
|
| while(true)
| {
| aresMq1= mq1.BeginReceive();
| aresMq2= mq2.BeginReceive();
| Process();
| }
|
| }
|
| void Process()
| {
|
| int iWaitIndex = WaitHandle.WaitAny(new
| WaitHandle[]{aresMq1.AsyncWaitHandle, aresMq2.AsyncWaitHandle});
|
| switch (iWaitIndex)
| {
|
| case 0:
| Message msg1 = mq1.EndReceive(aresMq1);
| Stream s1=PrepareMsg(msg1,mq1);
| writeMessage(s1,msg1.Label);
| break;
|
| case 1:
| Message msg2 = mq2.EndReceive(aresMq2);
| Stream s2=PrepareMsg(msg2,mq2);
| writeMessageEx(s2,msg2.Label);
| break;
|
| }
| }
|
|

Why not simply create a thread per message queue, you don't have hundreds of
queue's I suppose?
Also, don't use the threadpool from OnStart, create a regular thread.

Willy.
 
Top