Whats going on?

Can somwone tell me whats happening here, i have the following code to work out timers in c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Timers;


namespace WordTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Timers.Timer myTimer = new System.Timers.Timer();
            myTimer.Elapsed += new ElapsedEventHandler(Timer_Tick);
            myTimer.Interval = 1000;
            myTimer.Start();

        }



        public void Timer_Tick(object sender, EventArgs e)
        {

            label1.Text = "Time passed";
        }



    }
}


But i get the following error:

Cross-thread operation not valid: Control 'label1' accessed from a thread other than the thread it was created on

Whats going on?
LVL 1
icb01co2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
Form controls can be accessed only from the thread where they are created. To work with form controls from another thread use BeginInvoke or Invoke as described here:

http://www.codeproject.com/csharp/workerthread.asp
AlexFMCommented:
namespace WordTest
{
    public delegate void DelegateSetText(string s);

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Timers.Timer myTimer = new System.Timers.Timer();
            myTimer.Elapsed += new ElapsedEventHandler(Timer_Tick);
            myTimer.Interval = 1000;
            myTimer.Start();
        }

        ' runs in the context of timer thread
        public void Timer_Tick(object sender, EventArgs e)
        {
            ' call SetLabelText in the context of main thread
            this.BeginInvoke(new DelegateSetText(this.SetLabelText), new Object[] {"Time passed"});
        }

        void SetLabelText(string s)
        {
            label1.Text = s;
        }
       
    }
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
icb01co2Author Commented:
Thanks AlexFM, can i ask a few stupid questions cause im new to c#.

1) What is a delegate type?

2)I dont understand this syntax:

 new Object[] {"Time passed"}

Whenever ive instansiated a new ive used a constructor method i.e. SomeObject a = new SomeObject(constructorArguments).
What do the {} mean?

What im actually trying to do is have an array of words, and for every interval have a different word appear.

Thanks, Chris.
AlexFMCommented:
1. Delegate is pointer to function. Type of delegate is type of function which is called using this delegate. For example, DelegateSetText has void return type and 1 string parameter exactly as SetLabelText function. First BeginInvoke parameter is delegate which is initialized by SetLabelText function.

this.BeginInvoke(new DelegateSetText(this.SetLabelText), ...);

means: call SetLabelText asynchronously in the context of main thread.

2. Second BeginInvoke parameter has object[] type. This is generic parameter which is used to pass function arguments. In this case, we need to pass one string argument:
new Object[] {"Time passed"}
{} is C# way of array initialization. Instead of hard-coded "Time passed" you can use any variable.

icb01co2Author Commented:
Thanks for your time AlexFM.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.