This is the code:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
List<string> tempNamesAndTexts = new List<string>();
string tempDownload = downloadContent();
GetProfileNames(tempDownload);
GetTextFromProfile(tempDownload);
for (int i = 0; i < names.Count; i++)
{
tempNamesAndTexts.Add(names[i] + " " + texts[i]);
}
if (InvokeRequired)
{
BeginInvoke(new Action(() => tempNamesAndTexts.ForEach(Item => textBox1.AppendText(Item + Environment.NewLine))));
}
while (true)
{
namesAndTexts = 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++)
{
namesAndTexts.Add(names[i] + " " + texts[i]);
}
if (InvokeRequired)
{
bool result = tempNamesAndTexts.SequenceEqual(namesAndTexts);
if (result == true)
{
}
else
{
var t = namesAndTexts.Last();
if (textBox1.InvokeRequired)
{
BeginInvoke(new Action(() => textBox1.AppendText(t + Environment.NewLine)), null);
return;
}
}
}
reader.Close();
response.Close();
Thread.Sleep(30000);
}
}
}
The problem is in this line:
BeginInvoke(new Action(() => textBox1.AppendText(t + Environment.NewLine)), null);
return;
If i will put a reutn; or break; it will keep adding the variable t nonstop to the textBox. And i want it to be added only once. And the loop will continue but that the variable t will be added only once.
Hope I understand well your question, it sounds that you simply need to check on
This without
returnnaturally, so thewhileloop will continue to run.If this is not what you’re asking for, please clarify.