Sucessfully using the .NET class Screen
in a background thread in a WinForms application, I wondered wether this is well-defined behavior.
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.
So my question is:
Is it OK to read the Screen.PrimaryScreen.Bounds
property in a background thread?
Update 1:
Please note that my question is not about thread-safety. It is about accessing UI elements from a background thread.
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.
Update 2:
Thanks for downvoting and flagging to close this question.
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. - The class lives in the System.Windows.Forms assembly which might indicate it requires special attention.