首页 > 代码库 > Windows service 学习

Windows service 学习

最近看了看windows service,

1. 找到了一个帖子:http://blog.csdn.net/knight94/article/details/627298

2. At the end of the link http://www.cnblogs.com/Googler/archive/2013/07/23/3208354.html , there is a sentance: The "Interact with Desktop" option is not supported by Microsoft in Windows Vista and newer. So use it wisely and redesign your app if there is a solid chance that your service can be installed on Vista or Server 2008.

应该是因为这个原因:我在Win7中安装了一个windows service, 这个windows service的主要功能是启动一个windows Form app. 当启动这个service的时候,总是会报这个错误:
This problem occurs when a program is not fully compatible with Windows. Please contact the program or device manufacturer(s) for more information. in the Interactive Services Detection dialog.

3. windows service Properties:

 Log On ---> Allow service to interact with desktop.

4. Debug windows service:
   there is a way to debug the windows service:
 1). Modify "Output type" to "Windows Application" in the serivce project property tab page.
 2). in the Main(), to add the following code in Program.cs:

        static void Main()
        {
            if (Environment.UserInteractive)
            {
                string[] args = {"", ""};
                Service service = new Service();
                service.Start(args);
            }

            else
            {
                ServiceBase[] ServicesToRun;

                // More than one user Service may run within the same process. To add
                //another service to this process, change the following line to
                // create a second service object. For example,
                //
                ServicesToRun = new ServiceBase[] {new Service1(), new MySecondUserService()};
                //
                ServicesToRun = new ServiceBase[] { new Service() };

                ServiceBase.Run(ServicesToRun);
            }
        }