Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You've got a couple of options here.</p> <h2>Approach #1</h2> <p>First, you can use .NET's asychronous APIs for fetching the data. Assuming you're using <a href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx" rel="nofollow"><code>HttpWebRequest</code></a> you'd want to take a look at <a href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetresponse.aspx" rel="nofollow"><code>BeginGetResponse</code></a> and <a href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.endgetresponse.aspx" rel="nofollow"><code>EndGetResponse</code></a>, as well as the <a href="http://msdn.microsoft.com/en-us/library/system.io.stream.beginread.aspx" rel="nofollow"><code>BeginRead</code></a> and <a href="http://msdn.microsoft.com/en-us/library/system.io.stream.endread.aspx" rel="nofollow"><code>EndRead</code></a> methods on the <code>Stream</code> you get back the response.</p> <h3>Example</h3> <pre><code>var request = WebRequest.Create("http://someurl.com"); request.BeginGetResponse(delegate (IAsyncResult ar) { Stream responseStream = request.EndGetResponse(ar).GetResponseStream(); // use async methods on the stream to process the data -- omitted for brevity }); </code></pre> <h2>Approach #2</h2> <p>Another approach is to use the thread pool to do your work, rather than creating and managing your own threads. This will effectively cap the number of threads you're running, as well as removing the performance hit you'd normally get when you create a new thread. </p> <p>Now, you're right about not wanting to repeatedly fire updates while you wait for </p> <h3>Example #2</h3> <p>Your code might look something like this:</p> <pre><code>// We use a dictionary here for efficiency var Updating = new Dictionary()&lt;TheXMLObjectType, object&gt;; ... if (de &gt;= DateTime.Now) { return finalXML(); } else { // Lock the updating dictionary to prevent other threads from // updating it before we're done. lock (Updating) { // If the xml is already in the updating dictionary, it's being // updated elsewhere, so we don't need to do anything. // On the other hand, if it's not already being updated we need // to queue RefreshFinalXml, and set the updating flag if (!Updating.ContainsKey(xml)) { // Use the thread pool for the work, rather than managing our own ThreadPool.QueueUserWorkItem(delegate (Object o) { RefreshFinalXml(); lock(Updating) { Updating.Remove(xml); } }); // Set the xml in the updating dictionary Updating[xml] = null; } } return finalXML(); } </code></pre> <p>Hopefully that's enough for you to work off of.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload