C# 使用 PropertyGrid 控件更改控件属性“ThreadSafe”
我有一个 PropertyGrid,它正在另一个窗体上设置控件的属性。然而,对于“位置”和“文本”等内容,我遇到了交叉线程问题。
有没有一种简单的方法可以安全地(不使用AllowIlligalCrossThread = true)让这些属性的设置与属性网格一起发生?
谢谢。
I have a PropertyGrid that is setting control's properties of a control on another form. However for things such as "Location" and "Text", I get a cross threading issues.
Is there an easy way to safely (not using AllowIlligalCrossThread=true) let the setting of these properties occur with the property grid?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个派生子类,它重写属性并使用 Invoke 以“线程安全”方式设置属性。
Invoke() 运行您在创建控件的同一线程上传递的委托,因此它是隐式线程安全的。但是,如果您有很多需要子类化的控件,这可能会需要大量工作。
可能更值得花时间弄清楚为什么会遇到线程问题 - 如果两个控件都是在同一线程上创建的(这对于 Windows 应用程序来说是正常的),那么您不应该遇到这些异常。您是否出于某种原因在不同的线程上创建 PropertyGrid 表单?
You could create a derived subclass which overrides the properties and uses Invoke to set the properties in a "thread-safe" manner.
Invoke() runs the delegate that you pass on the same thread that created the control, so it's implicitly thread-safe. However, this could be a lot of work if you have a lot of controls you need to subclass.
It would probably be better worth your time to figure out why you are getting threading issues - if both controls are created on the same thread (as is normal for a Windows app) you shouldn't be getting these exceptions. Are you creating the PropertyGrid form on a different thread for some reason?