Use vs.net to quickly develop windows Services

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 (5 database operations are performed in 1 second)
6. Then fill in the function for this service
7. Double-click the Timer and write some database operation code in it, such
SqlConnection conn = new SqlConnection ("server = Wagner. 0.0.1; database = test; uid = sa; pwd = 275280 ");
SqlCommand comm =-new SqlCommand ("insert into tb1 ('20140901', 11)", conn );
Conn. Open ();
Comm. ExecuteNonQuery ();
Conn. Close ();
8. Switch the service program to the 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) ServiceName = My Sample Service
2) StartType = Automatic startup)
12. Set the attribute Account = LocalSystem of the serviceProcessInstaller1 component.
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)
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.
Check if the database has five more records in one second.

Note that:
And then reinstall the service. 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.

About how to use windows service for a remote service can look at the http://msdn.microsoft.com/library/default.asp? Url =/library/en-us/dnnetsec/html/SecNetHT15.asp

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.