Detecting if the Process is Interactive
Processes can run in user interactive mode, which allows user interface actions, such as displaying dialog boxes and windows. Windows services are generally not user interactive, so such communications are not permitted.
When you develop library code that may be used by other programmers, you cannot be sure of the environment in which it will execute. It is possible that it will be used within a Windows-based application with full user interactivity, or as part of a windows service with restrictions on the user interface elements that may be used.
Some library code might include interaction with the user via dialog boxes, windows or the command line. You might want such code to also support execution within a Windows service, where this would not be permitted. In such cases you might decide to obtain the required information through command line switches or configuration, rather than asking the user directly.
To determine whether user interaction is available or not, you can check the UserInteractive property of the Environment class. This static property returns true if the software is running is user interactive mode. If the code is executing within a service, such as a Windows service or within Internet Information Services, the property returns false.
The following code shows the result when executing in a console application:
Console.WriteLine(Environment.UserInteractive); // Outputs "True"
18 November 2013