C# Threading inside for loop
<p>I want the for loop to complete before I print the result, in:</p>

<pre><code>        for (int i = 0; i &lt; 5; i++)
        {
            (new System.Threading.Thread(() =&gt;
                {
                    if (TimeTakingMethod())
                    {
                        ++nResult;
                    }
                })).Start();
        }
        Console.WriteLine("Count = " + nResult);
</code></pre>

<p>but Console.WriteLine will not wait for those threads to finish because printing is done on the main thread.</p>

<p>If I change it to:</p>

<pre><code>        System.Threading.Thread t = new System.Threading.Thread(() =&gt;
            {
                for (int i = 0; i &lt; 5; i++)
                {
                    (new System.Threading.Thread(() =&gt;
                    {
                        if (TimeTakingMethod())
                        {
                            ++nResult;
                        }
                    })).Start();
                }
            });
        t.Start();
        t.Join();
        Console.WriteLine("Count = " + nResult);
</code></pre>

<p>it will still not solve the problem because the nested threads will not be waited for.</p>

<p>Any simple solution to this? Thanks for going through this.</p>