MessageBox.Show会导致跨线程异常吗?
我可以在子线程上调用 MessageBox.Show
而不必担心跨线程异常吗?我知道表面上的答案是“尝试一下并找出答案”,我成功地做到了这一点,但我注意到 Windows 7 在引发跨线程异常方面似乎不如 Windows XP 严格。
那么,是否有任何地方记录了可以安全地从子线程执行此操作?
Can I call MessageBox.Show
on a child thread without worrying about a cross-thread exception? I know the ostensible answer would be "try it and find out", which I did, successfully, but I've noticed that Windows 7 seems to be less strict about raising cross-thread exceptions than Windows XP.
So, is it documented anywhere that this would be safe to do from a child thread?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在没有所有者窗口的情况下调用
MessageBox.Show("text")
,那么您就可以保存。如果您像
MessageBox.Show(this,"text")
那样调用MessageBox.Show
并指定所有者窗口,情况就会有所不同。我已经看到,在 Windows XP 中,您可以保存,但在 Windows 7 中,如果所有者窗口运行在与您调用
MessageBox
的线程不同的线程中,您将遇到跨线程异常!If you call
MessageBox.Show("text")
without an owner window you are save.If you call
MessageBox.Show
likeMessageBox.Show(this,"text")
specifying the owner window things are different.I have seen that in Windows XP you are save but in Windows 7 you will run into a cross-thread exception if the owner window is running in a different thread than the thread from which you call the
MessageBox
!不会,它不会导致任何“跨线程”异常。这是因为 MessageBox 不是在 UI 线程上运行的类的成员。
No, it will not cause any "cross-thread" exceptions. That's because the MessageBox is not a member of a class running on the UI thread.
您可以安全地将 MessageBox 放入单独的线程中。但请记住,它只会阻塞创建它的线程。如果您使用的是 WinForm 应用程序,并且另一个线程调用 MessageBox,则仍然可以使用该表单,并且用户可以忽略它。
You are safe to throw a MessageBox in a separate thread. But keep in mind that it will only block the thread that it was created on. If you are using a WinForm app and another thread calls the MessageBox then the form is still able to be played around with and the user can ignore it.