Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8423081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:38:07+00:00 2026-06-10T03:38:07+00:00

I start by searching for a file name. Once a find it, I look

  • 0

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:

  • danny.xml
  • adi.xml
  • yaron.xml
  • shelly.xml

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

  • yaron.xml
  • yaron.xml
  • yaron.xml
  • yaron.xml

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);

        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:38:08+00:00Added an answer on June 10, 2026 at 3:38 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 600 tab-delimited .txt files that look like this: barcode gene.symbol value 1
I have an XML file which describes the data-structure that I can exchange on
The MANIFEST.MF file contains an entry to define which *.properties files are loaded at
Update: sample file sample workbook Problem : I would like to have something that
my first question here... I have been searching how solve this issue i start
I start a download using PHP with this code: <? $_REQUEST['file'] = urldecode($_GET['file']); header(Content-type:
When searching code for strings, I constantly run into the problem that I get
I want to open any file type by using the code below: System.Diagnostics.Process.Start(pathFile); Is
Hey so I've been searching around for this topic and couldn't find a whole
I need help in deciding with search algorithm to use for searching large files.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.