By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,948 Members | 1,595 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,948 IT Pros & Developers. It's quick & easy.

Polling on Mutliple MSMQs

P: n/a
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;

}
}

Mar 13 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Vai2000" <no****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
| 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.
Mar 13 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.