i have a method in another class that select all the lines that contains given char from textbox2,and print it to textbox3 on button_click but i'm having this error (textBox3 ACCESSED FROM A THREAD OTHER THAN THE THREAD IT WAS CREATED ON).
public void plinq()
{
List<string> lines = new List<string>(textBox2.Lines);
List<string> lines2 = new List<string>();
try
{
if (textBox3.InvokeRequired)
{
textBox3.Invoke(new Action(plinq));
}
else
{
lines.AsParallel().ForAll(K =>
{
for (int i = 0; i < K.Length; i++)
{
if (lines[i].Contains(textBox4.Text))
{
lines2.Add(lines[i]);
}
}
textBox3.Lines = lines2.ToArray();
});
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
and her is the code of the button where i am calling the method
private void button6_Click(object sender, EventArgs e)
{
textBox3.Text = "";
int n = Convert.ToInt32(textBox1.Text);
worker = new Worker(n, textBox2, textBox3, textBox4);
Thread thread = new Thread(worker.plinq);
sw.Start();
thread.Start();
//worker.plinq();
sw.Stop();
button6.Text = Convert.ToString(sw.Elapsed);
sw.Reset();
}