This is my first project on threading but i'm lost.. The problem is: I want to make a second counter, where every prime number turn color.
Thread thread1;
private void Form1_Load(object sender, EventArgs e)
{
thread1 = new Thread(new ThreadStart(voidPrime));
}
int sec=0;
void voidPrime()
{
sec+= 1;
label1.Text = sec.ToString();
if (isprime(int.Parse(label1.Text)))
{
label1.ForeColor = Color.RED;
}
else
{
label1.ForeColor = Color.Black;
}
}
private void PLAY_Click(object sender, EventArgs e)
{
thread1.Start();
while (thread1.IsAlive)
{
Thread.Sleep(1000);
}
}
When I start and press the PLAY button, throws me
Cross-thread operation not valid: Control 'label2' accessed from a thread other than the thread it was created on.