Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Edit: </p> <p>Ooops, I was also first confused. </p> <p>The problem is that it only looks aditive (accumulative) because the ElapsedTime values are always output in increasing order only. </p> <p>So, if I have, as in my demo below, in the order of launching: </p> <ul> <li>1st launched task's duration of 10 sec (10000 msec), </li> <li>the 2nd task's duration of 8 sec (8 000 ms), </li> <li>the 3d task's duration of 6 sec (6 000 ms), </li> </ul> <p>then the results appear in <strong>output</strong> out of initial order - ALWAYS in order of increasing duration of tasks: </p> <ul> <li>the 1st in output: the 3d launched task's duration (of 6 sec duration) </li> <li>the 2nd in output: the 2nd launched task's duration (of 8 sec duration) </li> <li>the 3d (last) in output: the 1st launched task's duration (of 10 sec duration) </li> </ul> <p>Here is the output from Console app below: </p> <pre><code>from DoSomething 6043 from main 6043 from DoSomething 8057 from main 8057 from DoSomething 10058 from main 10058 </code></pre> <p>And it is obvious why - because the faster task always finishes and output before longer (more time consuming) task. </p> <pre><code>using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using System.Diagnostics; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var iResult = new List&lt;Task&gt;(); for (int i=5; i&gt;2; i--) { int load = i; var task = Task.Factory.StartNew(() =&gt; DoSomething(load), TaskCreationOptions.LongRunning); //following commented lines do NOT change the behavior in question task.ContinueWith(m =&gt; Console.WriteLine("from main "+m.Result)); //iResult.Add(task); } Console.ReadLine(); } //public static myMsg DoSomething() public static long DoSomething(int load) { Stopwatch timer = System.Diagnostics.Stopwatch.StartNew(); //usage of either prev or following 2 lines produce the same results //Stopwatch timer = new Stopwatch(); //instead of prev .StartNew(); //timer.Start();// instead of prev .StartNew(); Console.WriteLine("***Before calling DoLongRunningTask() " + timer.ElapsedMilliseconds); Console.WriteLine("GetHashCode "+timer.GetHashCode()); DoLongRunningTask(load); timer.Stop(); long elapsed = timer.ElapsedMilliseconds; Console.WriteLine("from DoSomething "+ elapsed); return elapsed;//return new myMsg(timer.ElaspedMilliseconds); } public static void DoLongRunningTask(int load) { Thread.Sleep(2000*load); /******************* another variant of calculation intensive loading load = load; double result = 0; for (int i = 1; i &lt; load*100000; i++) result += Math.Exp(Math.Log(i) ); */ } } } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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