表单的 InvokeRequired == false 和包含控件的 InvokeRequired == true
这怎么可能?我有 Windows 窗体控件,派生自 System.Windows.Forms.Form,该窗体中包含 WebBrowser 控件。 Webbrowser 对象实例是在表单的构造函数中创建的(在 InitializeComponent() 方法中)。然后在后台线程中我操作WebBrowser的内容,我发现在某些情况下Form.InvokeRequired == false,而WebBrowser.InvokeRequired == true。怎么可能呢?
how is it possible? I have windows Form control, derived from System.Windows.Forms.Form with WebBrowser control contained in this form. Webbrowser object instance is created in constructor of form (in InitializeComponent() method). Then in background thread I manipulate with content of WebBrowser, and I found that in some cases Form.InvokeRequired == false, while WebBrowser.InvokeRequired == true. How can it be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在显示表单之前,
Form.InvokeRequired
返回false
。我做了一个简单的测试:
使用助手的
输出是
另请注意,这在 MSDN:
您的解决方案是还检查
IsHandleCreated
。编辑:
Handle
可以随时在 WebBrowser 控件内部或外部创建。这不会自动创建父窗体的句柄。我创建了一个示例:
输出:
Form.InvokeRequired
returnsfalse
before the form is shown.I did a simple test:
with the helper
the output is
Also note that this is somewhat documented on MSDN:
Your solution is to also check for
IsHandleCreated
.Edit:
The
Handle
can be created at any time internal in the WebBrowser control or externally. This does not automatically create the handle of the parent form.I created an example:
with the output:
以下是对相应且更普遍的问题的详细调查:http://www.ikriv .com/en/prog/info/dotnet/MysteriousHang.html
Here's detailed investigation of corresponding and more generic problem: http://www.ikriv.com/en/prog/info/dotnet/MysteriousHang.html
我一直在调查同样奇怪的行为。
我需要从不同的线程操作一些控件(例如,显示有关连接到主机的设备的信息或根据不同的设备状态触发操作)。
这个链接给了我一个很好的提示:
http://csharpfeeds.com/post/2898/Control.Trifecta_InvokeRequired_IsHandleCreated_and_IsDispose.aspx
我仍然不知道微软的人们打算如何利用他们自己的东西(并且在很多方面都不同意),但是,在一个应用程序中,我不得不做出以下肮脏的解决方法:
多么丑陋,不是吗?
我想知道其他人是否有更好的方法。
(很抱歉在这篇有点旧的帖子中发帖,但我只是认为它可能对某人有用)。
I've been investigating this same weird behaviour.
I need to operate some controls from different threads (e.g. show information about a device that is connected to the host or trigger actions depending on different devices states).
This link gave me a good hint:
http://csharpfeeds.com/post/2898/Control.Trifecta_InvokeRequired_IsHandleCreated_and_IsDisposed.aspx
I still don't know how MS people intended to make use of their own stuff (and don't agree in many aspects), but, in one application I had to make the following dirty and filthy workaround:
How ugly, isn't?
I'd like to know if anyone else has a better way to do it.
(Sorry for posting in this somewhat old post, but I just thought it could be usefull to someone).