如何使用Threading.Timer调用控件? [英] How do I invoke a control with Threading.Timer?

查看:74
本文介绍了如何使用Threading.Timer调用控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试触发在Threading.Timer上运行的方法.该方法执行一些计算,然后将结果显示在listView控件中.

示例:

Hi,

I''m trying to fire a method thats running on Threading.Timer. The method performs a few calculations then displays the results in a listView control.

Example:

Threading.Timer thrdTimer = new Threading.Timer(myMethod,null,0,1000);

private void myMethod(object obj)
{
   // do some calculations...
   // pass calculation values(val1,val2,val3) to listView

   listView.Items[0].SubItems[1].Text = val1;
   listView.Items[1].SubItems[2].Text = val2;
   listView.Items[2].SubItems[3].Text = val3;
}


推荐答案

您需要使用Control.Invoke在UI线程上调用方法-msdn文档
You need to use Control.Invoke to call a method on the UI thread - msdn documentation here[^].


希望我将在Abhinav引用的页面上解释调用比MSDN更好一点:

Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
Hope I''ll explain invocation just a bit better than MSDN on the page Abhinav referenced:

Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

I cannot believe you really need timer. In real life in most cases you should better use threading. For example, if you need to poll something periodically, create a thread and call what you want in cycle calling Thread.Sleep for timing.

Please see this link to my past answers:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


您应该使用BackGroundWorkder组件执行此类任务...

请参阅下面的链接
链接1 [链接2 [ Link3 [
You should use BackGroundWorkder Component to perform this kind of task...

see the below link
Link1[^]
Link2[^]
Link3[^]


这篇关于如何使用Threading.Timer调用控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆