title: Am I obliged to call EndPeek after using BeginPeek?
body: <p>I have a Windows service that processes a private, local message queue (MSMQ). When it starts, it registers an event handler for <code>PeekCompleted</code> on the queue, and it then calls the asynchronous <code>BeginPeek()</code> to wait for a message to arrive.</p>
  
  <pre><code>protected override void OnStart(string[] args)
  {
      if (String.IsNullOrWhiteSpace(args[0]))
          return;
  
      queue = new MessageQueue(args[0]);
      queue.Formatter = new BinaryMessageFormatter();
      queue.PeekCompleted += new PeekCompletedEventHandler(OnPeekCompleted);
  
      queue.BeginPeek();
  }
  </code></pre>
  
  <p>Once a message arrives, my goal is to obviously process that message. My code currently has a <code>queue.Receive()</code> method to get the message, contained within a transaction so the message gets put back on the queue in case of errors during processing. <code>BeginPeek()</code> is called again to restart the cycle.</p>
  
  <pre><code>private static void OnPeekCompleted(Object source, PeekCompletedEventArgs asyncResult)
  {
      try
      {
          MessageQueue q = (MessageQueue)source;
  
          using (MessageQueueTransaction trans = new MessageQueueTransaction())
          {
              trans.Begin();
  
              Message msg = q.Receive(trans);
              ProcessMessage(msg);
  
              trans.Commit();
           }
  
           // Restart the asynchronous peek operation.
           q.BeginPeek();
      }
      catch (MessageQueueException qEx)
      {
          // TODO: Walk it off.
      }
      return;
  }
  </code></pre>
  
  <p><strong>Do I, at any point, need to call <code>EndPeek()</code> on the queue ?</strong> </p>
  
  <p>Maybe to avoid memory leaks, like <a href="https://stackoverflow.com/questions/3093215/msmq-beginpeek-peekcompleted-memory-leak">this question</a> alludes to ? I'm pretty sure I don't have to, but the documentation isn't very clear on that. It just doesn't feel 100% right to 'begin' something without 'ending' it :)</p>
  
  <p>Btw: I could replace the <code>Receive()</code> line with <code>Message msg = q.EndPeek(asyncResult.AsyncResult)</code>, which equally fetches me the message, but it doesn't remove the message from the queue.</p>