C # create Windows Services)

Source: Internet
Author: User

Windows Services are called NT Services in earlier versions of Visual Studio, and new names are enabled in vs.net. Using Visual C # To create a Windows service is not difficult. This article will guide you step by step to create a Windows service and use it. When the service starts and stops, it writes some text information to a text file.

Step 1: create a service framework
To create a new Windows service, you can select the Windows Service option from the Visual C # project, give the project a new file name, and click OK.

You can see that the wizard adds the webservice1.cs class to the project file:

The meaning of each attribute is:

Ü autolog: whether to automatically write data to System Log Files

Ücanhandlepowerevent receives power events during the service

Ü whether the canpauseandcontinue Service accepts requests for suspending or resuming running the service

Ü whether the canshutdown Service receives a notification when the computer that runs it shuts down, so that it can call the onshutdown Process

Ü whether the canstop Service accepts the stop operation request

Ü servicename service name

 

Step 2: add features to the service
In. CSCodeFile, we can see that there are two ignored functions onstart and onstop.

The onstart function is executed when the service is started, and the onstop function is executed when the service is stopped. Here, when the service is started and stopped, some text information is written to a text file. The Code is as follows:

Protected override void onstart (string [] ARGs)

{

Filestream FS = new filestream (@ "D: \ mcwindowsservice.txt", filemode. openorcreate, fileaccess. Write );

Streamwriter m_streamwriter = new streamwriter (FS );

M_streamwriter.basestream.seek (0, seekorigin. End );

M_streamwriter.writeline ("mcwindowsservice: Service started" + datetime. Now. tostring () + "\ n ");

M_streamwriter.flush ();

M_streamwriter.close ();

FS. Close ();

 

}

 

Protected override void onstop ()

{

Filestream FS = new filestream (@ "D: \ mcwindowsservice.txt", filemode. openorcreate, fileaccess. Write );

Streamwriter m_streamwriter = new streamwriter (FS );

M_streamwriter.basestream.seek (0, seekorigin. End );

M_streamwriter.writeline ("mcwindowsservice: Service stopped" + datetime. Now. tostring () + "\ n ");

M_streamwriter.flush ();

M_streamwriter.close ();

FS. Close ();

}

Step 3: InstallProgramAdd to service application

Visual Studio. NET comes with the installation component that can be used to install resources associated with service applications. The installation component registers a single service on the system to which it is being installed, and notifies the Service Control Manager of the existence of the service.

To correctly install the service, you do not need to perform any special encoding in the installer. However, to add special features to the installation process, you may occasionally need to modify the content of the installation program.

To add an installer to a service application, follow these steps:

1: In the solution, access the design view of the service to which you want to add and install components.

2: In the Properties window, click Add installer Link

In this case, a new projectinstaller class and two installation components serviceprocessinstaller and serviceinstaller are added to the project, and the service attribute values are copied to the component.

3: To determine how to start the service, click the serviceinstaller component and set the starttype attribute to an appropriate value.

Ümanual must be started manually after the service is installed.

Ü automatic the service is automatically started every time the computer restarts.

Ü the disabled service cannot be started.

4: Change the account attribute of the serviceprocessinstaller class to LocalSystem.

In this way, no matter which user is logged on to the system, the service will always start.

 

Step 4: generate a service program

You can select generate from the generate menu to generate a project.

Do not run the project by pressing F5-you cannot run the service project in this way.

Step 5: Install the service

Access the directory of compiled executable files in the project.
Use the project output as a parameter to run installutil.exe from the command line. Enter the following code in the command line:
Installutil yourproject.exe
Uninstall Service

Use the project output as a parameter to run installutil.exe from the command line.

Installutil/u yourproject.exe

 

Note: If you need to connect to the database, you can only connect to the local database and cannot access other databases through IP addresses.

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.