解决方案 »

  1.   

    服务B启动的时候判断下服务A是否运行,比如通过Process.GetProcesses。
      

  2.   

    没搞明白需求
    你这叫什么"依赖关系"啊
    如果在B的代码里去判断A,此时其实B已经启动了,只不过不执行真正的处理逻辑而已或者你在A里写代码,A启动了就把B也启动,B设置成手动启动,而不是自动启动
      

  3.   

    用Mutex 
    http://msdn.microsoft.com/zh-cn/library/system.threading.mutex.aspx
      

  4.   


    那怎么在A里写代码可以启动B呢?我试了一下以下代码,但还是无法启动服务B
     protected override void OnStart(string[] args)
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] 
                { 
                    new ServiceB() 
                };
                ServiceBase.Run(ServicesToRun);        }
      

  5.   

    // Create the named mutex. Only one system object named 
            // "MyMutex" can exist; the local Mutex object represents 
            // this system object, regardless of which process or thread
            // caused "MyMutex" to be created.
            Mutex m = new Mutex(false, "MyMutex");        // Try to gain control of the named mutex. If the mutex is 
            // controlled by another thread, wait for it to be released.        
            Console.WriteLine("Waiting for the Mutex.");
            m.WaitOne();
    http://msdn.microsoft.com/zh-cn/library/f55ddskf%28v=vs.110%29.aspx
      

  6.   

    ServiceInstaller的对象有个属性叫ServicesDependedOn属性, 你可以把要依懒的服务名称写在里面,就OK了
      

  7.   


    ServiceInstaller的对象有个属性叫ServicesDependedOn属性, 你可以把要依懒的服务名称写在里面,就OK了