Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to signal waithandle.waitany when doing async requests?
    primarykey
    data
    text
    <p>I'm using the following sample code to fetch some HTML Pages using async requests.</p> <p>I don't want to wait until every request is completed that is using WaitHandle.WaitAll, just until the correct value is found. I'm currently doing it this way, but it feels wrong to send ManualResetEvents to the thread. Is it how it should be done? Is there a better way?</p> <pre><code> public static void runprogram() { System.Net.ServicePointManager.DefaultConnectionLimit = 20; FetchPageDelegate del = new FetchPageDelegate(FetchPage); List&lt;HtmlDocument&gt; htmllist = new List&lt;HtmlDocument&gt;(); List&lt;IAsyncResult&gt; results = new List&lt;IAsyncResult&gt;(); List&lt;WaitHandle&gt; waitHandles = new List&lt;WaitHandle&gt;(); List&lt;ManualResetEvent&gt; handles = new List&lt;ManualResetEvent&gt;(); for (int i = 0; i &lt; 20; i++) { ManualResetEvent e = new ManualResetEvent(false); handles.Add(e); } for(int i = 0; i &lt; 200; i += 10) { int y = 0; string url = @"URLTOPARSE" + i; IAsyncResult result = del.BeginInvoke(url, handles[y], null, null); results.Add(result); waitHandles.Add(result.AsyncWaitHandle); y++; } //Here i check for a signal WaitHandle.WaitAny(handles.ToArray()); //WaitHandle.WaitAll(waitHandles.ToArray()); foreach (IAsyncResult async in results) { FetchPageDelegate delle = (async as AsyncResult).AsyncDelegate as FetchPageDelegate; HtmlDocument htm = delle.EndInvoke(async); if(htm.DocumentNode.InnerHtml.Contains("ANYTHING TO CHECK FOR(ONLY A TEST")) { return; } } } </code></pre>
    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. 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