Very poor performance of async task run on threadpool in .Net native : <p>I've observed a strange difference in managed vs .Net native code. I've a heavy job redirected to threadpool. When running the app in managed code, everything works smooth but as soon as I switch on native compilation - the task run few times slower and so slow that it hangs UI thread (I guess CPU is so overloaded). </p>

<p>Here are two screenshots from debug output, the one on the left is from managed code, and the one on the right is from native compilation. As you can see the time consumed by UI task is nearly the same in both cases, up to a time when threadpool job is started - then in managed version UI elapsed time grows (in fact UI gets blocked and you cannot take any action). Timings of threadpool job speak for themselves. </p>

<p><a href="https://i.stack.imgur.com/8x7vvm.png" rel="noreferrer"><img src="https://i.stack.imgur.com/8x7vvm.png" alt="Managed"></a><a href="https://i.stack.imgur.com/qof4pm.png" rel="noreferrer"><img src="https://i.stack.imgur.com/qof4pm.png" alt="Native"></a></p>

<p>The sample code to reproduce the problem:</p>

<pre class="lang-cs prettyprint-override"><code>private int max = 2000;
private async void UIJob_Click(object sender, RoutedEventArgs e)
{
    IProgress&lt;int&gt; progress = new Progress&lt;int&gt;((p) =&gt; { MyProgressBar.Value = (double)p / max; });
    await Task.Run(async () =&gt; { await SomeUIJob(progress); });
}

private async Task SomeUIJob(IProgress&lt;int&gt; progress)
{
    Stopwatch watch = new Stopwatch();
    watch.Start();
    for (int i = 0; i &lt; max; i++)
    {
        if (i % 100 == 0) { Debug.WriteLine($"     UI time elapsed =&gt; {watch.ElapsedMilliseconds}"); watch.Restart(); }
        await Task.Delay(1);
        progress.Report(i);
    }
}

private async void ThreadpoolJob_Click(object sender, RoutedEventArgs e)
{
    Debug.WriteLine("Firing on Threadpool");
    await Task.Run(() =&gt;
   {
       double a = 0.314;
       Stopwatch watch = new Stopwatch();
       watch.Start();
       for (int i = 0; i &lt; 50000000; i++)
       {
           a = Math.Sqrt(a) + Math.Sqrt(a + 1) + i;
           if (i % 10000000 == 0) { Debug.WriteLine($"Threadpool -&gt; a value = {a} got in {watch.ElapsedMilliseconds} ms"); watch.Restart(); };
       }
   });
    Debug.WriteLine("Finished with Threadpool");
}
</code></pre>

<p>If you need a complete sample - then you can <a href="https://onedrive.live.com/redir?resid=87995A9E6AADB641!58603&amp;authkey=!AJz9OH5TWsnVe5k&amp;ithint=file%2czip" rel="noreferrer">download it here</a>.</p>

<p>As I've tested the difference appears on both optimized/non optimized code, in both debug and release versions.</p>

<p>Does anybody have an idea what can cause the problem?</p>
