So couple of things:
The sample code found on the GitHub site is designed to exit after it receives (and acks) at least one message. The code would look a little different for a real consumer application, and the main application thread would never exit.
I am guessing you are observing that the application is successfully pulling down several messages before exiting even though as-written it appears the code should only process one message per run.
This has to do with the speed of the thread switching. Calling WaitEventWaitHandle.Set()
does not actually immediately return control to the WaitEventWaitHandle.WaitOne()
calling site (which is on the main thread). The message handler dispatch thread will have sufficient time to continue pulling, processing, and acking before the application can exit.
You may be able to force the desired behavior by inserting a Thread.Yield()
call after setting the wait handle, but this is also not fully guaranteed to work in all cases.
I could provide sample code that can do different things, but what would you like to see the application do? Run and pull exactly one message off the queue, then exit?