Windows service program [solves some smart automatic needs]

Source: Internet
Author: User

In many applications, the Windows Service is required to perform some operations silently in the background, such:

1. Regular database backup.

2. Send regular emails or text messages.

Simply put, the system is able to do some things silently on a regular basis in the background. This may be done all the time or at intervals.

 

With vs2008, we can establish a Windows Service within several minutes, which is very simple.

The following are the steps

1. Create a "Windows Service" Project

2. Rename the default "service1" as your favorite name.

3. By default, vs opens the "design view" interface. In the blank area, right-click and select "attribute", and change "servicename" to your preferred name.

By default, this name is the name in the system "service.

4. Drag a system. Timer object from the component table in the toolbox to this design view.

Note: make sure that the system. Timer control is used instead of the system. Windows. Form. Timer control.

Otherwise, it is invalid and the service will not be effective after it is started.

By default, the timer control in the Toolkit "component" is exactly system. Windows. Form. Timer, so be sure to pay attention to it.

If the Toolkit does not contain the system. Timer control, right-click the Toolkit at any position-select item-find system. timer and add it.

5. Set the "interval" value of the timer control to the required time. The unit here is millisecond, 1 second = 1000 milliseconds.

6. Double-click the timer control. vs automatically switchesCodeInterface, write the required code here to complete what you want to do.

7. Switch to "design view", right-click in the blank area, and choose "add installation ".Program", The system automatically adds the projectinstaller component, and automatically opens the design view of the component. In this view, you can see two controls: serviceinstaller1 and serviceprocessinstaller1.

8. Right-click "serviceinstaller1"-"properties" and set the following parameters:

Servicename: the service name we set in step 3rd by default. Do not modify it. The default value is correct.

Starttype: The Service Startup type. You can set it as needed. during actual deployment, you can select automatic to prevent the server from restarting the service.

Dispalyname: indicates the friendly name displayed by the Service, which is equivalent to a nickname. If this parameter is not set, the name set in step 3 is used by default.

Description: A detailed description of the service, that is, a detailed description of the service.

9. Right-click "serviceprocessinstaller1"-"properties" and set the following parameters:

Account: The default value is user, but generally the user of this service is not specified. Instead, it is set as a local system service, that is, "LocalSystem ".

10. OK. Now we have finished our development work. The following describes how to install and deploy this service.

 

11. Run the installutil.exe command "C: \ WINDOWS \ Microsoft. NET \ framework \ v2.0.50727 \ Uninstall.

For example, installutil c: \ autoremindservice.exe (uninstall installutil c: \ autoremindservice.exe-U)

12. You can also package the package into an independent installer, which is suitable for installation in a foolproof manner and should be provided in the end.

The package can be a new independent project, or you can directly add a package project to the current solution. Here we will select the latter.

The following is a step-by-step description.

 

1. Select "file"-"add"-"New Project" from the vs menu ", in the displayed window, select "other project type"-"installation and deployment"-"Installation Project". You can enter the "name" of the Project as needed, by default, the "location" can be put together with the main project. You do not need to set it separately.

2. Right-click the added "installation project" Name and select "add"-"project output". In the displayed window, select the project to be deployed, generally, select "primary output" for the deployed file. The default value is "configuration.

3. Right-click the name of the newly added "installation project" and select "View"-"Custom operation ", on the displayed page, right-click "Custom operation" and select "add custom operation ", in the displayed window, select "main output from xxxxxx" in the "Application folder" and click "OK ".

4. Right-click the added "installation project" Name and select "View"-"User Interface", that is, set some parameters during installation.

This step does not matter, but if it is a formal installation program, it is necessary to set to display your company name or something.

5. Right-click the project you want to deploy, and right-click the installation project "regenerate ".

6. Find the directory where the installation project is located and copy the Installation File suffixed with MSI.

 

 

 

Note the following:

If you have modified this service and the path remains unchanged, you do not need to register the service again.

If the volume changes, uninstall the installutil.exe/u parameter and reinstall the service. In addition, the Windows Service has no interface. Do not output some messages in a controlled output mode. You can only add one EventLog and write logs through writeentry.

The Setting dialog box is also blocked. if the service is set to "allow service and desktop interaction", it is displayed only after the notification is enabled.

In fact, this is a service that runs silently in the background. Why do we need to interact?

If you only want the Service to provide you with information, you can enable the Service to automatically write files or send emails to you somewhere.

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.