A requirement is encountered in the project: The Windows service is installed automatically when the WinForm program is installed, and the Windows Service runtime in turn detects whether the WinForm program is started. If it is not, start.
After a review has been implemented under the WIN10 and normal operation. This record is easy to see later
Implementation ideas: Leveraging the packaged plug-in VS Installer there is a custom action that allows you to specify which assemblies to run after installation and start a Windows Service installation class by default in the assembly
Implementation steps:1. In the solution where the WinForm program is located, add a VS Installer packaging project, and the use of VS installer is no longer described, please Baidu.
2. Create a Class library project for custom actions
3. This is the service installation Help class for installing or removing services
Public classServicehelper {/// <summary> ///whether the service exists/// </summary> /// <param name= "ServiceName" ></param> /// <returns></returns> Public Static BOOLIsserviceexisted (stringserviceName) {servicecontroller[] Services=servicecontroller.getservices (); foreach(ServiceController sinchservices) { if(S.servicename = =serviceName) { return true; } } return false; } /// <summary> ///Start the service/// </summary> /// <param name= "ServiceName" ></param> Public Static voidStartService (stringserviceName) { if(isserviceexisted (serviceName)) {ServiceController service=NewServiceController (serviceName); if(Service. Status! = servicecontrollerstatus.running &&service. Status!=servicecontrollerstatus.startpending) {service. Start (); for(inti =0; I < -; i++) {service. Refresh (); System.Threading.Thread.Sleep ( +); if(Service. Status = =servicecontrollerstatus.running) { Break; } if(i = = -) { Throw NewException ("Start Service Error:"+serviceName); } } } } } /// <summary> ///Get Service Status/// </summary> /// <param name= "ServiceName" ></param> /// <returns></returns> Public StaticServiceControllerStatus Getservicestatus (stringserviceName) {ServiceController service=NewServiceController (serviceName); returnservice. Status; } /// <summary> ///Configure the service/// </summary> /// <param name= "ServiceName" ></param> /// <param name= "Install" ></param> Public Static voidConfigservice (stringServiceName,BOOLinstall) {Transactedinstaller ti=NewTransactedinstaller (); Ti. Installers.add (NewServiceProcessInstaller { account=Serviceaccount.localsystem,}); Ti. Installers.add (NewServiceInstaller {DisplayName=ServiceName, ServiceName=ServiceName, Description="Regular start of medical service", Servicesdependedon=New string[] { },//predecessor Service Servicesdependedon = new string[] {"MSSQLSERVER"}StartType = servicestartmode.automatic//operating mode }); Ti. Context=NewInstallContext (); stringstrpath =System.IO.Path.GetDirectoryName (assembly.getexecutingassembly (). Location); Ti. context.parameters["AssemblyPath"] = strpath +"\\"+"RestartService.exe";//assembly.getentryassembly (). Location if(install) {ti. Install (NewHashtable ()); } Else{ti. Uninstall (NULL); } } }
View Code
View Code
4. adding a class library for Windows services
///timed Task Client/// </summary> Public classAutotaskclient:registry { Publicautotaskclient () {Schedule<StartupWinfrom> (). Torunnow (). Andevery (1). Minutes (); } Public voidStart () {jobmanager.initialize ( This); } } /// <summary> ///timed start WinForm program/// </summary> Public classStartupwinfrom:ijob { Public voidExecute () {Process proc= Process.getprocessesbyname ("Xxxserver"). FirstOrDefault (); if(proc = =NULL) { stringPath = System.IO.Path.GetDirectoryName (assembly.getexecutingassembly (). Location) +"\\xxxServer.exe"; Cjwdevhelper.startprocess (path); } } }
View Code
Public Partial classService1:servicebase { PublicService1 () {InitializeComponent (); } protected Override voidOnStart (string[] args) { Newautotaskclient (). Start (); Filehelper.writetxt ("\ r \ n"+ DateTime.Now.ToString () +"Service started ...", Serverlogurl); } protected Override voidOnStop () {Filehelper.writetxt ("\ r \ n"+ DateTime.Now.ToString () +"service has stopped ...", Serverlogurl); } Internal Const stringServerlogurl ="ServerLog.txt"; }
View Code
5. finally add a custom action to the Installer packaging project:
Specify the output:
And then build a packaged project.
Installing Windows Services automatically when you install the WinForm program