Why I'm getting exception: Exception has been thrown by the target of an invocation?
<p>I have this code first the constructor and button click event:</p>

<pre><code>using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using System.IO;
using System.Collections;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        DirectoryInfo dirinf = new DirectoryInfo(@"C:\");
        List&lt;FileSystemInfo&gt; fsi = new List&lt;FileSystemInfo&gt;();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
            button1.Enabled = false;
        }
</code></pre>

<p>Then i have a search function and a backgroundowrker DoWork event:</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)));
                        }

                    }
                }

                    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);
            for (int i = 0; i &lt; fsi.Count; i++)
            {
                if (InvokeRequired)
                {
                    BeginInvoke(new Action(() =&gt; textBox1.AppendText(fsi[i - 1].FullName + Environment.NewLine)));
                }

            }
        }
</code></pre>

<p>When its getting to the part in the DoWork event: </p>

<pre><code>for (int i = 0; i &lt; fsi.Count; i++)
                {
                    if (InvokeRequired)
                    {
                        BeginInvoke(new Action(() =&gt; textBox1.AppendText(fsi[i - 1].FullName + Environment.NewLine)));
                    }

                }
</code></pre>

<p>After one or two itertions its throwing exception in the Program.cs on the line:</p>

<pre><code>Application.Run(new Form1());
</code></pre>

<p>Exception has been thrown by the target of an invocation</p>

<p>Solved it by reporting to the backgroundworker completed event:</p>

<pre><code>private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            Search(".xml", dirinf, fsi);
            backgroundWorker1.ReportProgress(100);

        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            for (int i = 0; i &lt; fsi.Count; i++)
            {                
                    textBox1.AppendText(fsi[i].FullName + Environment.NewLine);
            }
        }
</code></pre>

<p>Working just good.</p>
