c# - How can I cancel a Microsoft ServiceBus MessageReceiver Receive call? -


i have windows service written in .net 4 creates multiple threads contain infinitely running while(running) loops. when stop service, running boolean becomes false , break out of while loop , once each thread that, service stops.

inside while, calls omessagereceiver.receive(timespan.fromseconds(30)). when try stop service, have wait 30 seconds receive() process timeout.

one option lower timeout 30 seconds smaller. i'm not sure of performance penalties it'd incur, since process doesn't work without brokeredmessage, i'd keep listener open , long possible.

i see there .beginreceive() method on messagereceiver. have sense there programming pattern use here prevent service hanging while waiting receive timeout. can describe how use begin/end apm functions in infinitely running windows service?

close messagefactory messagereceiver created cancel receive operation. had same issue when calling abort or close on messagereceiver before closing message factory.

using asynchronous begin/end make no difference. side note if want run asynchronously consider creating task wrappers using taskfactory.fromasync take advantage of new async-await features.

also see answer on question regarding same issue:

cancel azure service bus queueclient long running receive?


Comments