Why am I getting in the textBox as results the same file name all the time?
<p>I start by searching for a file name. Once a find it, I look for other files. The first file is an .xls file, which is like a signature that I'm in the right directory. Then I get all the xml files in this directory.</p>

<p>I put a breakpoint on the line:</p>

<pre><code>BeginInvoke(new Action(() =&gt; textBox1.AppendText(pResult[i-1].FullName+Environment.NewLine)));
</code></pre>

<p>and I see 4 xml files:</p>

<ul>
<li>danny.xml </li>
<li>adi.xml  </li>
<li>yaron.xml   </li>
<li>shelly.xml</li>
</ul>

<p>But if I let the program run without any breakpoint then I only see the 3rd file in the List 4 times:</p>

<ul>
<li>yaron.xml </li>
<li>yaron.xml</li>
<li>yaron.xml</li>
<li>yaron.xml</li>
</ul>

<p>Can somone please point out why that might be happening?</p>

<pre><code>public void Search(string strExtension,
                            DirectoryInfo di,
                            List&lt;FileSystemInfo&gt; pResult)
        {
            try
            {

                foreach (FileInfo fi in di.GetFiles())
                {
                    if (InvokeRequired)
                    {
                        BeginInvoke(new Action(() =&gt; label2.Text = fi.Name));
                    }
                    if (fi.Name == "MessageLog.xsl")
                    {
                        foreach (FileInfo fii in di.GetFiles())
                        {
                        if (fii.Extension == strExtension)
                            pResult.Add(fii);
                        }
                        if (InvokeRequired)
                        {
                            BeginInvoke(new Action(() =&gt; textBox1.AppendText("Number Of History Files Found: ===&gt; " + pResult.Count.ToString() + Environment.NewLine)));
                        }
                        for (int i = 0; i &lt; pResult.Count; i++)
                        {
                            if (InvokeRequired)
                            {
                                BeginInvoke(new Action(() =&gt; textBox1.AppendText(pResult[i-1].FullName+Environment.NewLine)));
                            }

                        }
                    }
                }

                foreach (DirectoryInfo diChild in di.GetDirectories())
                    Search(strExtension, diChild, pResult);
            }
            catch (Exception e)
            {
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            Search(".xml", dirinf, fsi);

        }
</code></pre>
