开发者

Does the WinForms Control.Invoke rule apply to contained objects?

开发者 https://www.devze.com 2023-01-20 18:39 出处:网络
I understand that multi-threaded WinForms apps are required to use Control.Invoke or Control.BeginInvoke when accessing a control from a thread other than the UI thread.

I understand that multi-threaded WinForms apps are required to use Control.Invoke or Control.BeginInvoke when accessing a control from a thread other than the UI thread.

But does this rule also apply when manipulating objects that are contained within a c开发者_开发知识库ontrol but which do not derive from the Control base class?

For example, when using a WebBrowser control, is it OK to manipulate the DOM without using Control.Invoke?

Thanks, Tim


This applies to everything inheriting from Control.

By the way: you can just try this. It normally automatically throws under debug mode when you're accessing the methods illegally.


The answer is most definitely no. The reason is because you really have no idea when and how the Control will use the contained object and you certainly cannot inject the necessary synchronization mechanisms inside the Control. For example, what would happen if the Control needs to access the object from the WM_PAINT message at the same time you are manipulating it from a worker thread? You could put the necessary locks on your side, but there is pretty much nothing you could do to get the internal plumbing of the Control to do the same.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号