参考:https://docs.microsoft.com/zh-cn/dotnet/framework/windows-services/
#if DEBUG
Service1 myserver = new Service1();
myserver.OnDebug(); //在Service1.cs中添加 OnDebug()方法 再由这个方法进入OnStar()方法
//public void OnDebug()
//{
//OnStart(null);
//}
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite); // 让它一直保存线程不死
#else
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
#endif