title: Invoking operations asynchronously with expiration
body: <p>I have a classic asynchronous message dispatching problem. Essentially, I need to asynchronously dispatch messages and then capture the message response when the dispatch is complete. The problem is, I can't seem to figure out how to make any one request cycle self-expire and shortcircuit.</p>
  
  <p>Here is a sample of the pattern I am using at the moment:</p>
  
  <p><strong>Defined delegate for invokation</strong></p>
  
  <pre><code>private delegate IResponse MessageDispatchDelegate(IRequest request);
  </code></pre>
  
  <p><strong>Dispatch messages with a callback</strong></p>
  
  <pre><code>var dispatcher = new MessageDispatchDelegate(DispatchMessage);
  dispatcher.BeginInvoke(requestMessage, DispatchMessageCallback, null);
  </code></pre>
  
  <p><strong>Dispatch the message</strong></p>
  
  <pre><code>private IResponse DispatchMessage(IRequest request)
  {
    //Dispatch the message and throw exception if it times out
  }
  </code></pre>
  
  <p><strong>Get results of dispatch as either a response or an exception</strong></p>
  
  <pre><code>private void DispatchMessageCallback(IAsyncResult ar)
  {
    //Get result from EndInvoke(r) which could be IResponse or a Timeout Exception
  }
  </code></pre>
  
  <p>What I can't figure out is how to cleanly implement the timeout/shortcircuit process in the <strong>DispatchMessage</strong> method. Any ideas would be appreciated</p>