Sucessfully using the .NET class Screen
in a background thread in a WinForms application, I wondered wether this is well-defined behavior.
在WinForms应用程序的后台线程中成功使用.NET类屏幕,我想知道这是明确定义的行为。
I'm currently reading the pixel dimensions of the primary screen like:
我目前正在阅读主屏幕的像素尺寸,如:
var w = Screen.PrimaryScreen.Bounds.Width;
var h = Screen.PrimaryScreen.Bounds.Height;
The reason why I'm wondering is that the WinForms classes tend to be used from the foreground thread only, and I found no documentation stating whether the Screen
class is safe from using in a background thread or not.
我之所以想知道WinForms类只是从前台线程使用,我发现没有文档说明Screen类是否可以安全地在后台线程中使用。
So my question is:
所以我的问题是:
Is it OK to read the Screen.PrimaryScreen.Bounds
property in a background thread?
可以在后台线程中读取Screen.PrimaryScreen.Bounds属性吗?
Update 1:
更新1:
Please note that my question is not about thread-safety. It is about accessing UI elements from a background thread.
请注意,我的问题与线程安全无关。它是关于从后台线程访问UI元素。
E.g. using ILSpy I found that the methods internally use ReleaseDC
which might be disallowed or caused undefined results when being called from a background thread.
例如。使用ILSpy我发现这些方法在内部使用ReleaseDC,当从后台线程调用时,它可能被禁止或导致未定义的结果。
Update 2:
更新2:
Thanks for downvoting and flagging to close this question.
感谢downvoting和flagging来关闭这个问题。
I still do think that this might be non-obvious, since:
我仍然认为这可能是不明显的,因为:
- A background thread has no message pump and the
Screen
class might require one. - 后台线程没有消息泵,Screen类可能需要一个。
- The class lives in the System.Windows.Forms assembly which might indicate it requires special attention.
- 该类存在于System.Windows.Forms程序集中,这可能表明它需要特别注意。
1 个解决方案
#1
4
You cannot trust the .NET thread safety documentation, it is merely copy/pasta that was only ever is modified when it really mattered. The most important property of a monitor is that it doesn't jump around on you. There is no read-modify-write at play.
你不能相信.NET线程安全文档,它只是复制/意大利面,只有在真正重要时才会被修改。监视器最重要的特性是它不会跳到你身上。播放时没有读 - 修改 - 写。
Using it in a thread is fine, the class is a very thin wrapper around the MonitorFromXxx() winapi functions. The underlying DeviceIoControl calls are always safe, device drivers need to keep many processes happy. Actually using the results in a meaningful way, well, maybe not.
在线程中使用它很好,该类是MonitorFromXxx()winapi函数的一个非常薄的包装器。底层的DeviceIoControl调用始终是安全的,设备驱动程序需要让许多进程保持高兴。实际上以有意义的方式使用结果,好吧,也许不是。
#1
4
You cannot trust the .NET thread safety documentation, it is merely copy/pasta that was only ever is modified when it really mattered. The most important property of a monitor is that it doesn't jump around on you. There is no read-modify-write at play.
你不能相信.NET线程安全文档,它只是复制/意大利面,只有在真正重要时才会被修改。监视器最重要的特性是它不会跳到你身上。播放时没有读 - 修改 - 写。
Using it in a thread is fine, the class is a very thin wrapper around the MonitorFromXxx() winapi functions. The underlying DeviceIoControl calls are always safe, device drivers need to keep many processes happy. Actually using the results in a meaningful way, well, maybe not.
在线程中使用它很好,该类是MonitorFromXxx()winapi函数的一个非常薄的包装器。底层的DeviceIoControl调用始终是安全的,设备驱动程序需要让许多进程保持高兴。实际上以有意义的方式使用结果,好吧,也许不是。