Check `Environment.UserInteractive` before showing UI
See original GitHub issueIt doesnāt seem to affect WinForms. The checks come from WPF itself.
Perhaps winforms should add these checks š - no point in trying to show ux when
Environment.UserInteractiveis (genuinely)false.
_Originally posted by @vatsan-madhavan in https://github.com/dotnet/wpf/issues/2663#issuecomment-592207176_
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Environment.UserInteractive Property (System)
Gets a value indicating whether the current process is running in user interactive mode.
Read more >How to determine if starting inside a windows service?
Instead of using the Environment.UserInteractive property, modify the startup method of your service to check for a "-console" command lineĀ ...
Read more >Detecting if the Process is Interactive
To determine whether user interaction is available or not, you can check the UserInteractive property of the Environment class. This staticĀ ...
Read more >C# Program to Check Whether a Process is Running in ...
This property is used to check whether the process is running in user interactive mode or not. It will return true if the...
Read more >Trick the Environment.UserInteractive from command shell?
The doco says: The UserInteractive property reports false for a Windows process or a service like IIS that runs without a user interface....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Happened to notice this. The proposal would not work since as written, both Winformsā UserInteractive impl and the core libraries will return ātrueā (ie interactive) if GetUserObjectInformationW fails. It will fail on Windows Nano, and thus indicate interactive is true, and Nano certainly doesnāt support UI.
https://source.dot.net/#System.Windows.Forms/System/Windows/Forms/SystemInformation.cs,640 https://github.com/danmosemsft/runtime/blob/7a1ff8272bd8afe74ed1b98b8c7d1f6c6a6d2a07/src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs#L144
I donāt know about hte Winforms version, but the Environment API is essentially for code running on regular Windows SKUās to determine whether they are running within a non-interactive Windows Service.
The problem is that the borders are fluent: COM - STA - HWND - UI are all interconnected - at which point do you make the cut to āunsupportedā and at which point do you actually disallow a primitive when validating UserInteractive in WinForms? Unsupported only means you donāt get to ask for bugfixes, not that you arenāt allowed to do it, last time I heared about it VS puts the new style project system compiling against Desktop Framework under āunsupportedā yet still large parts of the ecosystem rely on it to work.
We have one service out of 20 or something that prints daily reports and requires WinForms to work. It already needs to be switched some flag to work in the first place, so it might be that for this service āUserInteractiveā would evaluate to true anyways. I donāt know the details but I could check.
We donāt encourage the usage of that service, but thereās certainly demand in running UI code āoffscreenā without a user logged on. Some customers want their daily reports printed and mailed automatically without having someone logged on permanently on the machine just so a process can run in UI context. The reporting library is winforms based and putting everything in a service is much easier than writing the interop required to allocate a window station and logon a dummy user via the win32 API (if thats even possible, I know there are APIs for things like this but never tried to do it, youād basically writing your own service infrastructure with UI support).
Devs are usually trying to solve a problem, declaring a solution āunsupportedā doesnāt matter, if its the ābestā solution the devs can find theyāll take it anyways. If you want to avoid that, offer them alternatives to solve their problem, rather than declaring it unsupported.
In general, many common libraries are written with UI context in mind yet there is still occasional demand running them in an automated fashion without having a user sitting in front of a screen. So you usually have the choice on going unsupported or reinvent everything, of course almost nobody can afford to do the latter.
Iām mentioning all that just as a data point since it came up in the discussion, I personally still have no preference over whether winforms should be usable in service context or not.