Daniel Lip
Daniel Lip

Reputation: 11317

Why am I getting in the textBox as results the same file name all the time?

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.

I put a breakpoint on the line:

BeginInvoke(new Action(() => textBox1.AppendText(pResult[i-1].FullName+Environment.NewLine)));

and I see 4 xml files:

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

Can somone please point out why that might be happening?

public void Search(string strExtension,
                            DirectoryInfo di,
                            List<FileSystemInfo> pResult)
        {
            try
            {

                foreach (FileInfo fi in di.GetFiles())
                {
                    if (InvokeRequired)
                    {
                        BeginInvoke(new Action(() => 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(() => textBox1.AppendText("Number Of History Files Found: ===> " + pResult.Count.ToString() + Environment.NewLine)));
                        }
                        for (int i = 0; i < pResult.Count; i++)
                        {
                            if (InvokeRequired)
                            {
                                BeginInvoke(new Action(() => 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);

        }

Upvotes: 0

Views: 93

Answers (1)

Habib
Habib

Reputation: 223277

The reason you are getting this behavior is:

BeginInvoke(new Action(() => textBox1.AppendText(pResult[i-1].FullName+Environment.NewLine)));

Your action is pointing to the memory location pResult[i-1], it does't copy the value, instead it hold the location of pResult with index i. Where as i after the loop execution is pointing to count. Suppose pResult[i-1] points to a memory location x012 then your action statement is holding:

textBox1.AppendText(from location x012)

now when you come out of the loop, it hasn't executed yet, it is just defined to append text from that particular memory location. That is why, when you are executing that action you are getting the last value for all actions.

You may see a similar brain teaser from Jon Skeet : http://www.yoda.arachsys.com/csharp/teasers.html

Upvotes: 1

Related Questions