Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
i am working with Timer class in c# when i call timer.Elapsed its show expection



C#
"Cross-thread operation not valid: Control 'dataGridView1' accessed from a thread other than the thread it was created on."


What I have tried:

C#
System.Timers.Timer timer = new System.Timers.Timer();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer.Interval = 5000;
            timer.Elapsed += Timer_Elapsed;
            timer.AutoReset = true;
            timer.Enabled = true;
        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            GetAll();
        }

        private void GetAll()
        {
            using (var db=new POSEntities())
            {
                dataGridView1.DataSource= db.Products.ToList();
            }
        }
Posted
Updated 9-Nov-16 2:17am
Comments
[no name] 9-Nov-16 8:14am    
Okay... and?

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900