0
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
{
    BackgroundWorker worker = sender as BackgroundWorker;
    while (true)
    {

        if ((worker.CancellationPending == true))
        {
            e.Cancel = true;
            break;
        }
        else
        {



            string content = downloadContent();
            GetProfileNames(content);
            GetTextFromProfile(content);
            for (int i = 0; i < names.Count; i++)
            {
                 namesAndTexts.Add(names[i] + " " + texts[i]);
            }
            if (InvokeRequired)
            {
                for (int f = 0; f < namesAndTexts.Count -1; f++)
                {
                    BeginInvoke(new Action(() => textBox1.AppendText(namesAndTexts[f])), null);
                }
            }
            reader.Close();
            response.Close();
            Thread.Sleep(1000);
        }
    }
}

I tried this part:

if (InvokeRequired)
{
    for (int f = 0; f < namesAndTexts.Count -1; f++)
    {
        BeginInvoke(new Action(() => textBox1.AppendText(namesAndTexts[f])), null);
    }
}

But its not working good its keeping adding the list over and over to the textBox. I want in the textbox to see it as:

daniel hello

yaron bye

danny good

And only once.

Another thing is if in the For im not doing namesAndTexts.Count - 1 but namesAndTexts.Count then its throwing me error exception the namesAndTexts[f] that the index should not be less then zero but less then the list....


After this will work I need to check if the List is changed and make somehow automatic push up in the textBox lets say to fill the textBox to its size with lines and when its getting to the bottom start scroll up and keep update the new lines only from the bottom abnd push the old ones up from the top.

How can i do it?

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
user1544479
  • 927
  • 5
  • 13
  • 21

4 Answers4

1

Replace these lines:

for (int f = 0; f < namesAndTexts.Count -1; f++)
{
     BeginInvoke(new Action(() => textBox1.AppendText(namesAndTexts[f])), null);
}

with these:

textBox1.Clear();
namesAndTexts.ForEach(Item=> textBox1.AppendText(Item + Environment.NewLine));
DelegateX
  • 719
  • 3
  • 8
0

Try placing your For loop inside your Action lambda. Another way is first summing up your string using the Sum LINQ function, and then put the result in your textbox.

RedFury
  • 11
  • 2
0

It seems you're trying to create a closure inside a loop and, when the code below is actually executed:

textBox1.AppendText(namesAndTexts[f])

it will use the current value of f, not the one it had the moment the lambda was created. That's why it adds the last element over and over again, and if you don't use namesAndTexts.Count - 1 the bounds check fails (because the current value of f is namesAndTexts.Count).

See also this question for a more detailed explanation of the "closure inside loop" problem. One way of avoiding it is by copying your f variable:

for (int f = 0; f < namesAndTexts.Count; f++)
{
    int fcopy = f;
    BeginInvoke(new Action(() => textBox1.AppendText(namesAndTexts[fcopy])), null);
}
Community
  • 1
  • 1
mgibsonbr
  • 21,755
  • 7
  • 70
  • 112
0

It's repeating because of this:

        while (true)
        {

               reader.Close();
                response.Close();
                Thread.Sleep(1000);
            }
        }
    }

Also, you can execute "textBox1.Clear", before this:

                        for (int f = 0; f < namesAndTexts.Count -1; f++)
                        {
                            BeginInvoke(new Action(() => textBox1.AppendText(namesAndTexts[f])), null);
                        }
Nathan
  • 2,705
  • 23
  • 28