如何从 System.Timers.Timer 访问表单控件(跨线程问题) [英] How to access a form-control from System.Timers.Timer (cross thread problem)

查看:42
本文介绍了如何从 System.Timers.Timer 访问表单控件(跨线程问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 system.timers.timer 用于我的服务.

I use the system.timers.timer for my service.

现在我构建了一个测试表单,我也在其中使用它.在 timer_Elapsed 事件中,我做了一些工作,想停止它需要的计时器(xxx ms)并将其写在表单控件上以显示.

Now I build a test-form in which I use it too. In the timer_Elapsed Event I do some work and want to stop the timer it need (xxx ms) and write it on the form control to show.

但是当我访问列表视图时,出现跨线程错误.

But when I access the listview, I get an cross thread error.

有什么想法吗?

推荐答案

如果要从主 UI 线程以外的线程访问控件,则需要使用 在要访问的控件上调用方法.

If you want to acces a control from a thread other than the main UI thread, you need to use the Invoke method on the control you want to access.

这篇关于如何从 System.Timers.Timer 访问表单控件(跨线程问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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