Q: How to make a label change its text every some time in C# windows forms I have a label that I want to update every 5 seconds. It should change from 1921 to 1922 onward till 1992. I have tried using a timer but it gave me an error about being accessed on the wrong thread. The code I used was:
    public partial class Form1 : Form
    {
        int x = 1921;
        public Form1()
        {
            InitializeComponent();
        }

        System.Timers.Timer myTimer = new System.Timers.Timer(1000);
        private void UpdateLabel(object sender, ElapsedEventArgs e)
        {
            label1.Text = x.ToString();
            x += 1;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            myTimer.Elapsed += UpdateLabel;
            myTimer.Start();
        }
    }


A: Try this:
private void UpdateLabel(object sender, ElapsedEventArgs e)
{
    //Invoke makes the UI thread call the delegate.
    Invoke((MethodInvoker)delegate {label1.Text = x.ToString(); });
    x += 1;
}


A: try this 
   private readonly object y = new object();
    int x = 1921;
    public Form1()
    {
        InitializeComponent();
    }

    System.Timers.Timer myTimer = new System.Timers.Timer(1000);
    private void UpdateLabel(object sender, ElapsedEventArgs e)
    {
        Invoke((MethodInvoker)(() => { lock (y) { label1.Text = x.ToString(); x++; } }));
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        myTimer.Elapsed += UpdateLabel;
        myTimer.Start();
    }

