VS is actually easy to create a Windows Service

Source: Internet
Author: User

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. AddCode

Vs automatically generates some code

The following code is displayed in service1.cs:

View code

Protected Override VoidOnstart (String[] ARGs)
{
}

Protected Override VoidOnstop ()
{
}

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:

View code

Protected Override VoidOnpause ()
{
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 canProgramFirst, specify the parameters of the Windows service.

When an installer is added, a projectinstaller. CS is automatically generated. In this file, there is an initializecomponent method, as shown below:

View code

   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: \ windows \ Microsoft. NET \ framework \ v4.0.30319, run cmd, enter

C: \ windows \ microsoft. Net \ framework \ v4.0.30319 \ installutil xxxx.exe press enter to complete the installation of Windows Services.

When uninstalling, enterC: \ windows \ Microsoft. NET \ framework \ v4.0.30319 \ installutil/u xxxx.exe press Enter.

PS: it is more convenient to save as a bat file for execution. See: http://www.cnblogs.com/nbwzy/archive/2007/05/30/764571.html

 

A simple Windows service is ready. Start opening your Vs and try it on your own.

 

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.