VS2015 c#.net4.6 Windows Timer service

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.