MSDN上面测试Window的方法(很好用)

如何:将 Windows 服务作为控制台应用程序运行

  1. 向你运行 OnStart 和 OnStop 方法的服务添加一个方法:

     
     
            internal void TestStartupAndStop(string[] args)
            {
                this.OnStart(args);
                Console.ReadLine();
                this.OnStop();
            }
    
  2. 按如下所示重写 Main 方法:

     
     
    static void Main(string[] args)
            {
                if (Environment.UserInteractive)
                {
                    MyNewService service1 = new MyNewService(args);
                    service1.TestStartupAndStop(args);
                }
                else
                {
                    // Put the body of your old Main method here.
                }
    
  3. 在项目属性的“应用程序”选项卡中,将“输出类型”设置为“控制台应用程序”。

  4. 选择“启动调试”(F5)。

  5. 若要将该程序再次作为 Windows 服务运行,请安装它并像通常启动 Windows 服务一样启动它。 不必恢复这些更改。

posted @ 2015-02-15 17:20  洛晨随风  阅读(393)  评论(0编辑  收藏  举报