Note that there are some explanatory texts on larger screens.

plurals
  1. POSet Timeout on Parallel.Foreach loop
    text
    copied!<p>I have to send a <code>List</code> of my custom object(<strong>"Dial"</strong>) to the client from my WCF service.This object has 5 properties:Id,Name,Type,Individual_avg,Dept_avg. The values for Id,Name and Type are coming from a table but the Individual_avg and dept_avg are coming from a 3rd party service.The requirement is: </p> <ol> <li>Load the static properties - Id,Name,Type from a table </li> <li>Call a method within the WCF service that gets the other 2 property values from the 3rd party service.</li> </ol> <p>I have to timeout the call to 3rd party service after 30 seconds.That is,if the service takes more than 30 seconds to respond,I have to send the objects with only the 3 properties fetched from the table(Id,Name,Type) and set the other 2 properties to null. For example,there are 5 <code>Dials</code> in the <code>List</code>. Spawn 5 threads to call the 3rd party service to get the 2 properties.If the timeout occurs after getting the values for 2 <code>Dials</code>,send the <code>List</code> with all properties set for these 2 <code>Dials</code> and only Id,Name and Type set for the other 3 Dials. I have the following code to achieve it.</p> <pre><code>public DialResponse GetDials(DialRequest req) { DialResponse response = new DialResponse(); var indiDials = new List&lt;DialDetails&gt;(); foreach (var c in indiDialIdToShow) { var indiObj = _repository.FindQueryable&lt;DashboardDial_Tbl&gt;().Where(p =&gt; p.DialId == c.DialIdToShow &amp;&amp; p.IsActive == true).Select(p =&gt; new DialDetails() { Id = p.DialId, Name = p.DialName, Type = p.DialType, }).SingleOrDefault(); indiDials.Add(indiObj); } var timeout = 30000; // 30 seconds var cts = new CancellationTokenSource(); var token = new CancellationToken(); token = cts.Token; var myOptions = new ParallelOptions { CancellationToken = cts.Token }; using (var t = new Timer(_ =&gt; cts.Cancel(), null, timeout, -1)) { Parallel.ForEach(indiDials, myOptions, c =&gt; { DialDetails newDial = c; ReadConfig(c.Id, userName, ref newDial); //send a reference to the "dial" object and set the 2 properties on the object }); } response.DialsList = indiDials; } private void ReadConfig(string userName,ref DialDetails dialObj) { //call the 3rd party service dialObj.Individual_avg = serviceResponse.someValue; dialObj.dept_avg = serviceResponse.anotherValue; } </code></pre> <p>To test the "timeout" functionality,I put a <code>Thread.Sleep(40000)</code> in <code>"ReadConfig"</code> method.This returns an empty list of "Dials" instead of the sending "Dials" with only 3 properties(Id,Name,Type) set. What changes should I make so that this piece of code works as intended? </p>
 

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