title: Implement an Async timeout in Silverlight?
body: <p>In Silverlight, say we start an async request:</p>
  <pre><code>var request = WebRequest.Create(uri);
  </code></pre>
  <p>and then wait for a response via a delegate</p>
  <pre><code>request.BeginGetResponse(getResponseResult =&gt; ...
  </code></pre>
  <p>How do we timeout this waitee, to deliver a time-out error signal to the delegate instead?  Use a timer?  (note: timeout options available in the .net framework are missing in the Silverlight version).</p>
  <p>To handle two messages arriving at the same time, we could introduce a Guid filter, and then ignore a 2nd message when/if it was delivered to the delegate.  Or in reverse (better), we register an expiring Guid so that the delegate can discard a second message -- at least some garbage collection is possible then (although the memory footprint of a delegate has got to be on the small side).</p>
  <p>What follows are some notes I've made.  I haven't reached a conclusion yet.  Any help much appreciated.</p>
  <hr />
  <p>My line of enquiry was going to be to implement a timer, notify the callback, and then <em>somehow</em> cancel waiting delegate in request.BeginGetResponse(...).</p>
  <p>Note:</p>
  <ul>
  <li><p>While the .Net Frame Work 4 implements a timeout on the WebRequest class, the Silverlight version does not.</p>
  </li>
  <li><p>&quot;System.Threading.Task.Wait Method (TimeSpan)&quot; is not available either</p>
  </li>
  </ul>
  <blockquote>
  <p>QUESTION 1: is there a better way to implement a timeout error to the same delegate target?</p>
  <p>QUESTION 2: To cancel the waiting delegate, is it sufficient to use &quot;request.BeginGetResponse(null)&quot;?</p>
  <p>QUESTION 3: will a non executed delegate (e.g. getResponseResult =&gt; ...) cause a small memory leak?  Is this just a minor concern?</p>
  </blockquote>
  <hr />
  <p>Information on creating a timer:</p>
  <ul>
  <li><p>System.Windows.Threading.DispatcherTimer</p>
  </li>
  <li><p><a href="http://blog.bodurov.com/How-to-Create-setTimeout-Function-in-Silverlight" rel="nofollow noreferrer">http://blog.bodurov.com/How-to-Create-setTimeout-Function-in-Silverlight</a></p>
  </li>
  </ul>
  <p>Some References:</p>
  <ul>
  <li><p><a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.binding.opentimeout(v=VS.95).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.binding.opentimeout(v=VS.95).aspx</a>    (seems to be WCF related)</p>
  </li>
  <li><p><a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.binding.receivetimeout(v=VS.95).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.binding.receivetimeout(v=VS.95).aspx</a> (seems to be WCF related)</p>
  </li>
  <li><p><a href="http://blog.bodurov.com/How-to-Create-setTimeout-Function-in-Silverlight" rel="nofollow noreferrer">http://blog.bodurov.com/How-to-Create-setTimeout-Function-in-Silverlight</a>  (could be useful)</p>
  </li>
  <li><p><a href="https://stackoverflow.com/questions/2307844/silverlight-dealing-with-async-calls">Silverlight, dealing with Async calls</a>   (no new information)</p>
  </li>
  <li><p><a href="http://petesbloggerama.blogspot.com/2008/07/omg-silverlight-asynchronous-is-evil.html" rel="nofollow noreferrer">http://petesbloggerama.blogspot.com/2008/07/omg-silverlight-asynchronous-is-evil.html</a>  (I want to deal with Async properly... I've got some good techniques, but I want to ensure that I'm using them cleanly.  Probably I need to look into delegates more closely).</p>
  </li>
  <li><p><a href="http://msdn.microsoft.com/en-us/library/dd460717.aspx#CommunityContent" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/dd460717.aspx#CommunityContent</a></p>
  </li>
  <li><p><a href="http://csharperimage.jeremylikness.com/2010/03/sequential-asynchronous-workflows-in.html" rel="nofollow noreferrer">http://csharperimage.jeremylikness.com/2010/03/sequential-asynchronous-workflows-in.html</a>  (some good information.  Still haven't found out what to do about infinitely waiting delegates).</p>
  </li>
  </ul>