Writing Windows Service Memos

Source: Internet
Author: User

Project requirements to do a timed sweep table, the condition of the query to the data inserted or updated to another data table requirements, the boss asked to use the window service to do

Because I have not done it before, write this experience. As a memo.

1. What is a Windows service

The following sections are from http://www.cnblogs.com/salomon/archive/2012/05/21/2511643.html

Windows Services, Microsoft's official definition is this: Microsoft Windows Services (that is, previous NT services) enable you to create long-running Windows sessions in their own

Executable Application. These services can be started automatically when the computer starts, can be paused and restarted, and does not display any user interface. These features make the service very suitable for use on the server whenever

This is also true if you need to use features that do not affect other users who work on the same computer. You can also run a service in a different security context than a specific user account or a default computer account that is a logged-on user.

This description is too abstract and general. So what exactly is a service? First, the Windows service is an application, a background process, just like a normal app. But it's very special, especially in the

The following points:

1. It is usually loaded by the SCM (Service Control Manager) in net kernel before the user logs on to Windows at system startup, and is typically turned on automatically when the system starts.

The 2.Windows service is independent of a specific user, meaning it can be shared by any user on a computer.

If an application takes a long time to run in the background and is independent of a particular user console, we can consider writing it as a Windows service. It usually has no interface (there is no hard limit, can be compiled

A Windows service that writes an interface), usually randomly started and closed randomly, but can also be started manually with the user's needs, generally two methods: 1 Control Panel-Administrative Tools

Services 2, enabled by other programs to invoke the interface provided by SCM. Similar to Windows services, things like Unix daemons,dos run on Autoexec.bat or CONFIG. SYS's TSR. Common services include hardware drivers (printers, etc.), FTP services, database services, Web services, and so on.

2. Writing

2.1 First create a Windows service project

, we select the Windows Service project when creating the project. To create

2.2 Writing business logic code

We can also rename the service class. The OnStart method here is called when the service is started,

So we can write the business logic here. It is recommended to encapsulate a class and method to encapsulate the business logic operation, and then call the method of the class here

The OnStop method is called when the service is stopped, and we can write some processing logic here

3. Set the service name and logon permissions

Right-click on the design interface of the service-add installer. A ProjectInstaller.cs class is added.

There are two things we need to pay attention to here.

A. Setting the startup type of the service for ServiceInstaller1 controls in the design interface

automatic--Auto-start

manual--Manual Start

B. Setting service startup logon rights

Here we choose to start with the permissions of the local system.

4. Installation Services

Windows Services Microsoft offers its own installation program InstallUtil.exe

Run the program as an administrator, and then install your compiled service

CMD will alert you if the service is successfully installed

5. View service Status

5. Delete a service

During the debugging process, we may need to remove the service and then modify the code. Then reinstall the service and debug again.

Note that the service no-build failure must be removed first.

Note that the service must be stopped before the service can be deleted. Otherwise the deletion fails.

Recommended to open two CMD do not close one install one delete,

6. Commissioning the service

When we do, we want to test that the business logic is consistent with what we expect, but debugging services is a hassle.

We can be in Program.cs.

static void Main ()        {            servicebase[] servicestorun;            ServicesToRun = new servicebase[]             {                 new Service1 ()             };            Servicebase.run (ServicesToRun);        }

Call your business class here. New comes out and calls directly

The following comment is dropped.

The main function is also the portal for this service to start.

After we have installed the service on the machine and started it, we can also debug the service to run in a way attached to the process

Writing Windows Service Memos

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.