Vs.net development Windows Service (C #)

Source: Internet
Author: User

In many applications, Windows services are required to operate databases, such
(1) Some very slow database operations, don't want to do it all at once, and want to do it slowly through regular service, such as regular database backup.
(2) Use Windows Service for host in. Net remoting

With vs.net, we can establish its Windows service in a few minutes, which is very simple.

The following are the steps
1. Create a project
2. Select a Windows service from the list of available project templates
3. The designer opens in design mode.
4. Drag a timer object from the component table in the toolbox to the design surface (Note: Make sure that timer is used from the component list rather than from the Windows form List)
5. Set the timer attribute. The interval attribute is 200 milliseconds.

6. Then fill in the function for this service
7. Double-click the timer and write some operations in it.CodeFor example

Code

String Logfilepath =   " D: \ service.txt " ;
String Strmsg =   " Test " ;
Using (Filestream FS =   New Filestream (logfilepath, filemode. append, fileaccess. Write ))
{
Using (Streamwriter SW =   New Streamwriter (FS, encoding. Default ))
{
Sw. writeline (strmsg );
}
}

 

8. Set this serviceProgramSwitch to design view
9. Right-click the design view and select "add installer"
10. Switch to the design view of the newly added projectinstaller.
11. Set the attributes of the serviceinstaller1 component:

1) Description: My first Windows Service Program

2) displayname: mywindowstest
3) servicename: mywindowstest

4) starttype: Automatic startup)
12. Set the attribute account = LocalSystem of the serviceprocessinstaller1 component.

Here, serviceprocessinstaller1 and serviceinstaller1 have several important attributes to be set. starttype in serviceinstaller1 must be set to automatic, indicating random start, servicename indicates service name, description indicates service description, and displayname indicates display name. Set the account in serviceprocessinstaller1 to LocalSystem, indicating the local system account.


13. Change the path to the bin \ debug folder of your project (if you compile in release mode, in the bin \ release folder)
14. Execute the command "installutil.exe mywindowsservice.exe" to register this service so that it can create a suitable registration item. (Installutil is in the Windows Folder \ Microsoft. NET \ framework \ v1.1.4322)

Installutil.exe/u uninstall the service
15. Right-click "my computer" on the desktop and select "manage" to access the computer management console.
16. In the "services" section of "services and applications", you can find that your Windows services are included in the Service list.
17. Right-click your service and choose start to start your service.

Note:
you cannot directly install the service after you install the kernel/u parameter. In addition, the Windows Service has no interface. Do not output some information in the controlled output mode. You can only add one EventLog and write logs through writeentry.

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.