Check if I can show a dialog (based on current code is running in a winform app OR windows service)
I have some shared code I need to use both in a WinForm and a service. How can I test if my code is running in a service, so I开发者_运维知识库 can avoid problems with modal dialogs.
It might be enough for you to test the Environment.UserInteractive property, and only show the dialog if this is true.
System.Environment.UserName
isSYSTEM
orNetwork
then most possibly it is a service.Check callstack and see if very first method called belongs to the class derived from
ServiceBase
You could also check
Process.SessionId
to be 0 because all system processes & services run under session 0.
精彩评论