I am seeing the following error in Visual Studio within a class called CreateAlarm.cs tied to a user control.
'Cross-thread operation not valid: Control 'CreateAlarm' accessed from a thread other than the thread it was created on.'
This code is called on the following method:
private void ClearPanel()
{
panel1.Controls.Clear();
}
The way the program operates is that this line of code is the method that clears a panel of user controls, before more user controls are added by calling the method on the main form from within a user control.
The code that calls this method that is causing problems (when the error is caused) is the following:
void CreateAlarm_TimeElapsed()
{
ClearPanel();
Alarm alarmScreen = new Alarm(); // this creates an instance of the user control
AddToPanel(alarmScreen);
}
Where this code is actually called from within a user control with events and delegates.
The problem is otherwise identical code for a separate page is causing problems. I am thinking it could be because the code is run by a timer ending using System.Timers.
Does anyone have any ideas as to what could be the problem? Or additional information about the potential causes of the error message?
Thanks :)