当我调用timer.elapsed它的show expection时,我正在使用C#中的timer类 [英] I am working with timer class in C# when I call timer.elapsed its show expection
本文介绍了当我调用timer.elapsed它的show expection时,我正在使用C#中的timer类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
i am working with Timer class in c# when i call timer.Elapsed its show expection
"Cross-thread operation not valid: Control 'dataGridView1' accessed from a thread other than the thread it was created on."
我尝试了什么:
What I have tried:
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();
}
}
推荐答案
参见 c# - 获取错误使用Timer类时的跨线程操作 - Stack Overflow [ ^ ]。
这篇关于当我调用timer.elapsed它的show expection时,我正在使用C#中的timer类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文