How to determine if starting inside a windows service?

前端 未结 6 797
半阙折子戏
半阙折子戏 2021-02-15 16:37

Currently I\'m checking it in the following way:

if (Environment.UserInteractive)
    Application.Run(new ServiceControllerForm(service));
else
    ServiceBase.R         


        
6条回答
  •  既然无缘
    2021-02-15 17:20

    Instead of using the Environment.UserInteractive property, modify the startup method of your service to check for a "-console" command line argument. If the argument is present, then run as an ordinary application. If not, run as a service. It's not as automated as the property check, but it'd be easy to add a shortcut to the desktop that adds the "-console" command line argument for you.

    As an aside, you need to be aware that interaction with the desktop has been disabled in Windows Vista and beyond. If you are running a Windows service that needs to interact with the user, the approved way of doing this now is to separate your front-end application from your Windows service and have them communicate using something like WCF.

    If you need to debug your Windows service (whether it's running as a service or as an application), put a call to System.Diagnostics.Debugging.Break() in your startup method. This will force a prompt that allows you to enter a debugging session. I use this technique to debug my Windows service all the time.

提交回复
热议问题