c# - remove a defined number of messages from a message queue -
rather using .purge()
, there way delete defined number of messages queue?
i've tried setting messageenumerator
, using .removecurrrent
after i've done whatever need current message not seem work.
thanks
public message[] get10(messagequeue q) { int counter = 0; int mcount = 0; list<message> ml = new list<message>(); try { messageenumerator me = q.getmessageenumerator2(); while (me.movenext()) { counter++; } if (counter > 10) { mcount = 10; } else { mcount = counter; } counter = 0; me.reset(); { me.movenext(); counter++; ml.add(me.current); me.removecurrent(); } while (counter < mcount); } catch (exception x) { console.writeline(x.message); } message[] m = ml.toarray(); return m; }
when call removecurrent()
, enumerator moved next message. not have call movenext()
after calling removecurrent()
.
another approach may try following:
list<message> ml = new list<message>(); int count = 0; while( count < 10 ) { ml.add(me.removecurrent()); ++count; }
in case, must aware removecurrent
wait forever if there no messages left in queue. if not want, may want use removecurrent(timespan timeout)
overload , catch messagequeueexception
thrown in case of timeout. messagequeueexception
class has messagequeueerrorcode
property set messagequeueerrorcode.iotimeout
if timeout has expired.
or (this at most 10 messages: loop exit if message count in queue drops zero):
list<message> ml = new list<message>(); int count = 0; while( me.movenext() && count < 10 ) { ml.add(queue.receivebyid(me.current.id)); ++count; }
Comments
Post a Comment