private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
while (true)
{
List<string> tempNamesAndTexts = new List<string>();
if ((worker.CancellationPending == true))
{
e.Cancel = true;
break;
}
else
{
string content = downloadContent();
GetProfileNames(content);
GetTextFromProfile(content);
for (int i = 0; i < names.Count; i++)
{
tempNamesAndTexts.Add(names[i] + " " + texts[i]);
namesAndTexts.Add(names[i] + " " + texts[i]);
}
if (InvokeRequired)
{
bool result = tempNamesAndTexts.SequenceEqual(namesAndTexts);
if (result == true)
{
}
else
{
foreach (string com in tempNamesAndTexts)
{
if (!namesAndTexts.Contains(com))
{
MessageBox.Show(com);
}
}
//BeginInvoke(new Action(() => namesAndTexts.ForEach(Item => textBox1.AppendText(Item + Environment.NewLine))));
}
}
reader.Close();
response.Close();
Thread.Sleep(1000);
}
}
}
I have a List<string> namesAndTexts in the Form1 level.
And another temp local List<string> tempNamesAndTexts
What I need to do is in DoWork I’m updating the namesAndTexts every n seconds from a websites using the two functions in the DoWork.
In the end I have the namesAndTexts List for example:
[0] Daniel hello
[1] Moses hi
I need to compare this namesAndTexts with the tempNamesAndTexts List and each time when its updating the List if they are the same don’t do anything.
But if they are not the same find what was different in the two Lists and this different part only this add to the textBox.
The problem is now that namesAndTexts all the time grow and grow 18 then 36 then 72 indexes its keeping adding the same strings over and over.
What I need to do is:
-
Download the content from a website every n seconds (downloadContent();)
-
Create a List of names the GetProfileNames function.
-
Create a List of texts GetTextFromProfile function.
-
Create a List of the two Lists in 2. and 3. with names and text together.
-
Store the old List in 4. to remember the last update.
-
compare the last update with the new one. Compare the List in 5. with the one in 4.
-
If the comparison result is true identical don’t do anything.
-
If the comparison is not identical find which indexes in both Lists are not the same and add to the textBox the strings in the List which are not the same with the other one.
So the textBox should look like something, first once adding the List to the textBox for example:
Daniel is here
Modes is there
Daniela is out
Second time after comparison if its not identical find which index/s is not identical and add only the string in this index/s to the textBox.
So now the textBox will look like:
Daniel is here
Modes is there
Daniela is out
Yaron hello
So the only change was that Yaron hell is now added to the textBox.
But I can’t figure out how to do it.
Cant make the comparison of the which indexes are not the same and take them out and add them to textBox cant figure out why one of the Lists keep growing up so in the next iterations the two lists are not identical but also there are no new indexes with names and texts to add to the textbox.
You could find added or removed items thru
ExeptConsole output: