How to compile a Windows Service Program in Asp.net Visual Studio

Source: Internet
Author: User

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

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.