I have simple Windows Form App in C# using VS 2019. When I run the application using F5, I face the exception
"System.InvalidOperationException: 'Cross-thread operation not valid: Control 'richTextBox1' accessed from a thread other than the thread it was created on.'".
But when I use Ctrl+F5 everything works fine. Can anybody explain me why ?
Here is my code:
private async void button1_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
for (int i = 0; i < 500; i++)
{
Thread.Sleep(1000);
richTextBox1.Text += string.Format("\n Row No: {0}", (i + 1));
}
});
Task.Run(() =>
{
for (int i = 0; i < 500; i++)
{
Thread.Sleep(1000);
richTextBox2.Text += string.Format("\n Row No: {0}", (i + 1));
}
});
}