每2分钟所以我创建用户控件用的dataGridView,我实际上将如何对象是次要的,但让我们说我有alredy数据源,我想刷新的dataGridView这些值。如何调用函数的用户控件在.NET 3.5
的例子,我有功能fillDataGridView(),我想每2分钟
我认为我可以用Thread类中做到这一点,但它叫W/O任何成功还
如何你处理UI刷新?
我知道这看起来像“但与UI更新问题另一个人”,但是从我所看到的我真的不是最简单的办法做到这一点
public partial class Alertbox : UserControl
{
private static System.Timers.Timer aTimer;
public Alertbox()
{
InitializeComponent();
aTimer = new System.Timers.Timer(10000);
aTimer.Elapsed += new ElapsedEventHandler(Update);
aTimer.Interval = 2000;
aTimer.Enabled = true;
}
public void Update(object source, ElapsedEventArgs e)
{
BT_AddTrigger.Text += "test"; // append to button text
}
}
它喊这么
System.Windows.Forms.dll中发生类型'System.InvalidOperationException'异常,但未在用户代码中处理其他 信息:跨线程操作无效:控制'BT_AddTrigger' 从非线程访问它创建的线程。
为了防止错扣,使用:'BT_AddTrigger.Invoke(新行动(UpdateButtonText),新的对象[] {文本});'喜欢这里:HTTPS:/ /gist.github.com/PopovMP/8f747dbd6948eccc69ad。但是,更好的选择是使用'System.Windows.Forms.Timer',因为你在UserControl中。 –