1. Create a project
C#-> Classic Desktop->windows Service
2. Create a timer service
Find service 1, automatically generated OnStart (run when service is on), OnStop (run when service shuts down) two functions, we need to create a new timer and binding execution function on opening
protected override void OnStart (string[] args)
{
Timer1 = new System.Timers.Timer ();
Timer1. Interval = 3000; Set Timer event interval execution time
Timer1. Elapsed + = new System.Timers.ElapsedEventHandler (timer1_elapsed);//The name of the executed function that is bound in parentheses
Timer1. Enabled = true;
}
Execute function:
private void Timer1_elapsed (object sender, System.Timers.ElapsedEventArgs e)
{
}
3. Add the Installer
A service1.cs[design is opened by default when it is created] (how to open this page is not found), in the blank space on this page, right-click Add Installer
Right-click the ServiceProcessInstaller1 property, set account to LocalSystem
If you need to start the boot automatically, you also need to right-click ServiceInstaller1 settings StartType to Automatic
4. Installation Services
So the service is ready, we right-click the project--build
In the project folder->bin->debug can find the generated EXE file, save the path of this file
cmd into the directory where the installation service program InstallUtil, the default installation path is as follows:
C:\Windows\Microsoft.NET\Framework\v2.0.50727
C:\Windows\Microsoft.NET\Framework\v4.0.30319
Because we're using. NET 4.6 When we built it, so here's the following path.
CD C:\Windows\Microsoft.NET\Framework\v4.0.30319
InstallUtil Service Path \yourproject.exe
Open Services, find your service, right-click, start
ok~
If the service program has an update and needs to uninstall the service, uninstall the command:
installutil/u Service Path \yourproject.exe
VS2015 c#.net4.6 Windows Timer service