C#中的Windows服务无法及时启动

时间:2014-06-26 21:13:35

标签: c# windows-services

我搜索并搜索了Windows服务信息,它几乎不存在或过时。此外,VS 2013中没有Windows服务模板(我可以找到)。

我正在制作一个简单的性能监视器,它将CPU和RAM记录到文本文件中。我跟着几本过时的教程,自己想出了一些东西。

当我尝试通过F5(同事的建议)运行时,命令提示符闪烁打开,关闭然后程序结束。我认为不会调用OnStart方法。

我可以从VS命令提示符处安装好服务但是在尝试启动该过程时,我收到一个错误,它没有及时启动。我甚至尝试在服务管理器中启用与桌面的交互。

我也尝试过Debug和Release版本。

我已经查看了其他SO问题,建议在OnStart方法中进行所有初始化,我认为我做了(虽然我可能错了 - 我显然还在学习)。

相关代码:

namespace SystemMonitorD
{
    public class SystemMonitorD : ServiceBase
    {
        private Timer StateTimer { get; set; }
        private TimerCallback TimerDelegate { get; set; }
        private SystemMonitorL SysMon { get; set; }

        public SystemMonitorD()
        {
            ServiceName = "SystemMonitorD";
            CanStop = true;
            CanPauseAndContinue = true;
            AutoLog = true;
        }


        protected override void OnStart(string[] args)
        {
            SysMon = new SystemMonitorL();
            TimerDelegate = SysMon.Log;
            StateTimer = new Timer(TimerDelegate, null, SysMon.WaitTime, SysMon.WaitTime);
        }

        protected override void OnStop()
        {
            SysMon.StatusLog("Stop");
            StateTimer.Dispose();
        }

        protected override void OnPause()
        {
            SysMon.StatusLog("Pause");
            StateTimer.Change(Timeout.Infinite, Timeout.Infinite);
        }

        protected override void OnContinue()
        {
            SysMon.StatusLog("Continue");
            StateTimer.Change(SysMon.WaitTime, SysMon.WaitTime);
        }

        public static void Main()
        {
        }
    }

    public class SystemMonitorL
    {
        private readonly String _fileLocation = @"C:\Users\ian.elletson\Desktop\logD.txt";

        public int WaitTime { get; private set; }
        private IOutput Logger { get; set; }
        private List<SystemMonitor> SystemMonitors { get; set; }

        public SystemMonitorL()
        {
            WaitTime = 1000;
            Logger = new Logger(_fileLocation);
            SystemMonitors = new List<SystemMonitor>
            {
                SystemMonitorFactory.MakeSystemMonitor("CPU"),
                SystemMonitorFactory.MakeSystemMonitor("RAM")
            };
            Logger.WriteLine(string.Format("Polling every {0} second(s)", WaitTime / 1000));
        }

        public void Log(Object stateObject)
        {
            foreach (var monitor in SystemMonitors)
            {
                Logger.WriteLine(monitor.ToString());
            }
        }

        public void StatusLog(String status)
        {
            String message;
            switch (status)
            {
                case "Stop" :
                    message = "stopped";
                    break;
                case "Pause" :
                    message = "paused";
                    break;
                case "Continue":
                    message = "continued";
                    break;
                default:
                    message = "ERROR";
                    break;
            }
            Logger.WriteLine(string.Format("Logging {0} at {1}", message, TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Now)));
        }
    }

    [RunInstaller(true)]
    public class SystemMonitorDInstaller : Installer
    {
        ServiceProcessInstaller ProcessInstaller { get; set; }
        ServiceInstaller ServiceInstaller { get; set; }

        public SystemMonitorDInstaller()
        {
            ProcessInstaller = new ServiceProcessInstaller();
            ServiceInstaller = new ServiceInstaller();

            ProcessInstaller.Account = ServiceAccount.LocalSystem;
            ServiceInstaller.StartType = ServiceStartMode.Manual;
            ServiceInstaller.ServiceName = "SystemMonitorD";

            Installers.Add(ServiceInstaller);
            Installers.Add(ProcessInstaller);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

在调试Windows服务时,让生活更轻松的一件事就是为您的服务使用Debug \ Release标志。单步作为非服务的逻辑。

static void Main()
{
#if (!DEBUG)
    //RELEASE FLAG  
    System.ServiceProcess.ServiceBase[] ServicesToRun;
    ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MyService() };
    System.ServiceProcess.ServiceBase.Run(ServicesToRun);
#else
    //DEBUG                
    MyService service = new MyService(); //<--Put breakpoint here before you run your service
    service.OnStart(null);    
    System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#endif 
}

答案 1 :(得分:0)

我发现了我的问题。我失踪了 我的Main()方法中的ServiceBase.Run(new SystemMonitorD());。这解决了这个问题。我是从this MSDN link找到的。