首页 新闻 会员 周边 捐助

C#写一个Windows服务程序,启动多个service的问题

0
悬赏园豆:10 [已解决问题] 解决于 2012-03-01 22:36
需求如下:我希望用c#写一个windows   service程序,这个service程序里需要处理几种不同请求。      我的实现:添加一个windows   service类型的类maincontroller,修改代码如下           
static void main()
{
system.serviceprocess.servicebase[] servicestorun;
// run all user service in this process.
servicestorun = new system.serviceprocess.servicebase[] { new maincontroller(),
// main service
new service1 (),
new service2 (),
new service3 (),
};
system.serviceprocess.servicebase.run(servicestorun);
}

再向项目中添加windows service类型的类service1, service2, service3。查看代码可以发现service1,service2,service3大部分和maincontroller相同,但少了main()方法。添加相应代码到service1的onstart()、onstop()方法中,编译成功,然后安装运行该服务。但在查看服务输出时,发现maincontroller运行正常,但service1,service2,service3都没有被自动启动,除非在他们的构造函数里,主动调用onstart()。
跟着阿笨一起玩.NET的主页 跟着阿笨一起玩.NET | 初学一级 | 园豆:9
提问于:2011-12-17 01:25
< >
分享
最佳答案
0

你可以通过操作ServiceController实例来实现启动、暂停、停止、重启等。

http://www.cnblogs.com/downmoon/archive/2007/12/29/1019924.html

收获园豆:10
邀月 | 高人七级 |园豆:25475 | 2011-12-19 16:30
其他回答(2)
0

应该是在服务管理里面设置为自动启动吧!你也可以调用让他启动。

小小刀 | 园豆:1991 (小虾三级) | 2011-12-17 09:28
0

.net 里有个服务管理类;你可以根据服务名停止;启动等操作;

  public static void StartService(string serviceName)         {             if (IsServiceExisted(serviceName))             {                 System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);                 if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running &&                     service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)                 {                     service.Start();                     for (int i = 0; i < 60; i++)                     {                         service.Refresh();                         System.Threading.Thread.Sleep(1000);                         if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)                         {                             break;                         }                         if (i == 59)                         {                             throw new Exception("Start Service Error\n" + serviceName);                         }                     }                 }             }         }

        public static void StopService(string serviceName)         {             if (IsServiceExisted(serviceName))             {                 System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);                 if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)                 {                     service.Stop();                     for (int i = 0; i < 60; i++)                     {                         service.Refresh();                         System.Threading.Thread.Sleep(1000);                         if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)                         {                             break;                         }                         if (i == 59)                         {                             throw new Exception("Stop Service Error\n" + serviceName);                         }                     }                 }             }         }

画方软件 | 园豆:778 (小虾三级) | 2011-12-21 17:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册