question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Check `Environment.UserInteractive` before showing UI

See original GitHub issue

It 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.UserInteractive is (genuinely) false.

_Originally posted by @vatsan-madhavan in https://github.com/dotnet/wpf/issues/2663#issuecomment-592207176_

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
danmoseleycommented, Aug 14, 2020

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.

1reaction
weltkantecommented, Mar 6, 2020

Even though we repeatedly tell developers not to run WPF from within Windows services and that it’s unsupported

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.

they do so anyway; perhaps this happens in the WinForms ecosystem as well?

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found