Preface a data migration project was created some time ago. At the beginning, a project was created using the B/S architecture, but B/S needs to be stored in IIs, while IIs is unstable, if you restart IIs, you must open the page to run the project. If there is any inconvenience, use the Windows service instead. This article summarizes the compilation, debugging, installation, and uninstallation of windows Services. Windows Services introduction Microsoft Windows Services can create executable applications that can run for a long time in their own Windows sessions. These services can be automatically started when the computer is started. They can be paused and restarted without displaying any user interface. This makes the service very suitable for use on the server, or any time, in order not to affect other users working on the same computer, it needs to be used for a long time to run the function. You can also run the service in a security context different from the logon user's specific user account or default computer account. This article introduces how to use Visual C # To create a Windows Service Program for file monitoring step by step, and then describes how to install, test and debug the Windows service program. After creating a project for Windows Services, double-click Service1.cs and choose a design page. Right-click the project and choose add installer in the pop-up dialog box, you can modify the windows Service name and startup method. [RunInstaller (true)] public class Installer1: System. Configuration. Install. Installer {// <summary> // The required designer variable. /// </Summary> private System. componentModel. container components = null; private System. serviceProcess. serviceProcessInstaller spInstaller; private System. serviceProcess. serviceInstaller sInstaller; public Installer1 () {// This call is required by the designer. InitializeComponent (); // TODO: after InitComponent is called, add any initialization} # region Component Designer generated code // <summary> // The Designer supports the required methods-do not use the code editor to modify this method //. /// </Summary> private void InitializeComponent () {components = new System. componentModel. container (); // create ServiceProcessInstaller object and ServiceInstaller object this. spInstaller = new System. serviceProcess. serviceProcessInstaller (); this. sInstaller = new System. serviceProcess. serviceInstaller (); // set the account, user name, and password of the ServiceProcessInstaller object. this. spInstaller. account = System. serviceProcess. serviceAccount. lo CalSystem; this. spInstaller. username = null; this. spInstaller. password = null; // set the service name this. sInstaller. serviceName = "PmsDataUpdateService"; // service description this. sInstaller. description = "hi longhao! "; // Set the service startup mode. this. sInstaller. startType = System. serviceProcess. serviceStartMode. automatic; this. installers. addRange (new System. configuration. install. installer [] {this. spInstaller, this. sInstaller}) ;}# endregion} after modification, write the operation you want. Service1.cs: The design page is displayed. Double-click the design page to enter the cs code page. You can override these methods. Protected override voidOnStart (string [] args) {// service enabling Execution Code} protected override void OnStop () {// service end Execution Code} protected override void OnPause () {// The service suspends the execution of the code base. onPause ();} protected override void OnContinue () {// restore the Execution code base. onContinue ();} protected override void OnShutdown () {// The system is about to close the Execution code base. onShutdown ();} There is a Program. cs file: Open it. To make a Windows service program run normally, we need to create a program entry point for it just like creating a general application. In the Windows service program, we also completed this operation in the Main () function. First, create a Windows service instance in the Main () function. The instance should be a subclass object of the ServiceBase class, then we call a Run () method defined by the base class ServiceBase class. However, the Run () method does not start the Windows service program. We must use the service control manager mentioned earlier to call specific control functions to start the Windows service program, that is, the service will not really start running until the OnStart () method of the object is called. If you want to start multiple services in a Windows Service Program at the same time, you only need to define the Instance Object of multiple ServiceBae class subclasses in the Main () function, the method is to create an array object of the ServiceBase class so that each object corresponds to a pre-defined service. /// <Summary> /// main entry point of the application. /// </Summary> static void Main () {ServiceBase [] ServicesToRun; ServicesToRun = new ServiceBase [] {new Service1 (), new Service2 ()}; ServiceBase. run (ServicesToRun);} If you have written the required method in the function you need, click Run to Run it.