VS Development Windows Service Program

Source: Internet
Author: User

First, VS2012 under the development of Windows services

1, open VS2012, New project, select Windows Services, here I develop a timed automatic mail service to do the introduction, such as:

2, created, the compiler will automatically create some files, which will have a Service1 class file, which is the key to the service, we can rename it, here I named it as Mainservice, such as:

3, in the background code of Mainservice will automatically create some methods, including service start and stop methods, such as:

4, put the operation to be performed when the service starts in the OnStart method, the operation to be performed when the service is stopped is placed in the OnStop method

5. In MainService.cs Designer, click Mail and choose Add Installer to generate a ProjectInstaller.cs file:

6, set the properties of the ProjectInstaller.cs, switch to the property settings interface

(1) Select ServiceInstaller1 and set the corresponding properties as shown:

Where: The Description property is used to set the description information displayed on the service interface after the service is installed;

The DisplayName property is used to set the service name that is displayed on the service interface after the service is installed;

The ServiceName property is used to set the name of the service, which must be the same as the name of the ServiceName property setting for the ServiceBase class, which is used to identify the service and can be interpreted as an alias DisplayName servicename;

The StartType property is used to set how the service is started.

(2) Select ServiceProcessInster1 and set its properties as follows:

This is the main setting for the Account property, which is used to indicate the type of accounts when the service is running and is generally set to LocalSystem.

7, to this step a simple Windows service is created, the compiler will generate a EmailService.exe executable file, the next is the installation service.

Second, installation of Windows services

There are several ways to install a Windows service, such as a cmd-down command to install, or a setup program to install, which is described here using the bat batch.

1, install the Windows service needs to use the InstallUtil.exe program, the program is located in:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319. NET4.0

C:\Windows\Microsoft.Net\Framework64\v2.0.50727. NET2.0

2, we can re-batch command directly call the InstallUtil.exe program in the above directory, you can also copy the program to the application directory, where I choose to award the program copy to the application directory, while the batch file is also created in the application's directory, as shown in:

3. The batch command for the registration service is as follows:

%~dp0\installutil.exe%~dp0\emailservice.exe

Net Start Emailservice--This is where the service starts after installation is complete

sc config Emailservice start= auto– here is the service set to start automatically

Pause

Where%~dp0 represents the directory in which the batch is processed;

4. The batch command for the logoff service is as follows:

%~dp0\installutil.exe/u%~dp0\emailservice.exe

Pause

5. The batch command to start the service is as follows:

net start Emailservice

Pause

6. The batch command to stop the service is as follows:

net stop Emailservice

Pause

7, when we run the Registration Service batch command after the service installation, if the installation is successful, we will see the service in the service interface, as shown in:

8. Precautions when installing the service

(1) The version of the installer InstallUtil.exe must be the same as the. NET version that created the Windows service.

(2) If the system is Win7 or above, run as administrator when running the batch command;

(3) If the system is Win7 or above, special attention should be paid to the path of the installer and the application in order to be able to write batch commands correctly.

Reproduced in original: http://blog.csdn.net/huangcailian/article/details/42237013

VS Development Windows Service Program

Related Article

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.