Using VS for a windows service is actually very simple.
The following is a simple example of using VS2010 for windows Services and some precautions.
1. Create a windows Service
2. Add code
Vs automatically generates some code
The following code is displayed in Service1.cs:
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
OnStart is the method called when the service is started; OnStop is the method called when the service is stopped.
There are other methods for each service, such as pause and recovery, which can be implemented by reloading the methods of the base class, such as pause:
protected override void OnPause()
{
base.OnPause();
}
You can call and implement your own business logic in these methods.
Ps: During windows Services, it is often necessary to create a loop and process some business logic. A common method is to use a Timer control. It is worth noting that the Timer control in Toolbox is System by default. windows. forms. timer, obviously it won't work here, and System is available here. timers. timer. You can right-click the Toolbox to add it.
3. add Installer
This step is critical. After processing the business logic, you need to add an Installer so that your windows service can be installed.
Adding an Installer to VS is also very easy. The operation is as follows:
Right-click Service1.cs
Right-click the View designer View.
The Installer is added.
4. Set service parameters
You can specify the parameters of the windows service in the program.
When an Installer is added, a ProjectInstaller. cs is automatically generated. In this file, there is an InitializeComponent method, as shown below:
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.ServiceName = "Service1";
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
}
This. ServiceProcessInstaller1.Password= Null;
This. ServiceProcessInstaller1.Username= Null;
If you do not want to set the user name and password, you can use a local system account to run the service. The Code is as follows:
This. serviceProcessInstaller1.Account = System. ServiceProcess. ServiceAccount. LocalSystem;
Other settings can also be completed here.
5. Install and uninstall windows Services
After the windows service is released, it is an exe file. to install and use the service on the target machine, you can use the installutil tool provided by Microsoft to install and uninstall the service through command lines.
Run the installutil tool in the directory: System Disk: WINDOWSMicrosoft. NETFrameworkv4.0.30319, run cmd, enter
C: WINDOWSMicrosoft. NETFrameworkv4.0.30319installutil xxxx.exe press enter to complete the installation of windows Services.
When uninstalling, enter