开发者

Can MessageBox.Show cause cross-thread exceptions?

开发者 https://www.devze.com 2023-02-19 18:10 出处:网络
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,

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?


If you call MessageBox.Show("text") without an owner window you are save. If you call MessageBox.Show like MessageBox.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!


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.


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.

0

精彩评论

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