积极答复者
在threading.timers的定时器中如何修改被绑定的属性?

问题
答案
-
Hi chysx,
抱歉,我没有看懂你的具体需求是什么。但如果你是想跨线程修改控件的属性,你可以尝试使用以下代码关闭与UI线程交互的保护机制。
Control.CheckForIllegalCrossThreadCalls = false;
Regards,
Kyle
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已标记为答案 chysx 2019年7月9日 12:24
全部回复
-
Hi chysx,
抱歉,我没有看懂你的具体需求是什么。但如果你是想跨线程修改控件的属性,你可以尝试使用以下代码关闭与UI线程交互的保护机制。
Control.CheckForIllegalCrossThreadCalls = false;
Regards,
Kyle
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已标记为答案 chysx 2019年7月9日 12:24
-
感谢您的解答。
public void BindMainData(FrmMainModel model)
{
projectName.DataBindings.Add("Text", model, "Title");//标题
currentTime.DataBindings.Add("Text", model, "CurrentTime");//时间}
其中,currentTime是一个label, FrmMainModel 是实现InotifyChangedProperty接口的类 ,具备属性通知功能。
我将model的CurrentTime属性绑定到该label控件的Text上,然后用threading.timers空间中的Timer作为定时器,每1s去更新这个属性值,实现当前时间显示。所以问题在于当我在定时器中更改model的CurentTime属性,就会出现跨线程操作的问题,不知道我现在是否描述清晰,您刚才提到的解决方案不是我真正想要的方式,是否还有其他的解决办法?