1. Create> Project> Visual C #> Windows service;
2. We need to use timer components in general service programs. We recommend that you do not use system. windows. forms. timer component, because it may cause unexpected "strike" issues; should use system. timer component; you can write the following in the onstart process of the Service:
Protected override void onstart (string [] ARGs)
{
Timer timer = new timer (1000 );
Timer. elapsed + = new elapsedeventhandler (doanything); // use the elapsed event. doanything is what you need to handle.
Timer. autoreset = true;
Timer. Enabled = true;
}
Private void doanything (Object sender, system. Timers. elapsedeventargs E)
{
... Write the things you need to do here
}
3. After the installation is completed, you need to install it to the Windows service. Therefore, you need to add> new project> Installation Program class in the existing project, named projectinstaller. CS; in the class, two more controls are required: serviceinstaller and serviceprocessinstaller. the. NET 2.0 package is included, but is not displayed in the toolbox by default. You can add "select items" from the toolbox and find them. Serviceinstaller can set the display name, description, and running mode of the service (it is recommended to set it to automatic: automatic) and other information. serviceprocessinstaller is used to set the account identity for running the service. It is recommended to set it: localSystem (Local Account );
After everything is ready, CTRL + ALT + B compile the program, and the service program is basically done here! At this time, the service program is compiled and not finally installed in the Windows service. You also need to write a batch to register the service program. The following provides the batch file code.
Installservice. BAT (Registration and startup)
============================
C: \ windows \ microsoft. Net \ framework \ v2.0.50727 \ installutil.exe absolute path of the Service Program
Net start service name
Uninstallservice. BAT (uninstall and stop)
============================
Net stop service name
C: \ windows \ microsoft. Net \ framework \ v2.0.50727 \ installutil.exe-u absolute path of the Service Program