This document is excerpted from the Internet. Link to the original article: Http://developer.51cto.com/art/200908/144084.htm
C # Windows ServicesProgramRapid development: 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 describes how to quickly develop a C # Windows service program.
C # quick development of Windows service programs 1. Create a project
C # quick development of Windows service programs 2. Select Windows Services from an available project template list
C # quick development of Windows service programs 3. The designer will open in Design Mode
C # quick development of Windows service programs 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)
C # quick development of Windows service programs 5. Set the timer attribute, interval attribute 200 milliseconds (5 database operations in 1 second)
C # quick development of Windows service programs 6. Then add functions for this service
C # quick development of Windows service programs 7. double-click the timer and write the Code for database operations, for example,
- Sqlconnection conn = new sqlconnection (
- Server = 127.0.0.1;Database= Test; uid = sa; Pwd = 275280 ");
- Sqlcommand comm =-New sqlcommand (
- Insert IntoTb1 ('123', 11) ", Conn );
- Conn.Open();
- Comm. executenonquery ();
- Conn.Close();
C # quick development of Windows service programs 8. Switch this service program to the design view
C # quick development of Windows service programs 9. Right-click the design view and select "add installer"
C # quick development of Windows service programs 10. Switch to the design view of the newly added projectinstaller
C # quick development of Windows service programs 11. Set the properties of the serviceinstaller1 component:
1) servicename = My Sample Service
2) starttype = Automatic startup)
C # quick development of Windows service programs 12. Set the attribute account = LocalSystem of the serviceprocessinstaller1 component
C # quick development of Windows service programs 13. Change the path to the bin \ debug folder of your project (if you compile in release mode, in the bin \ release folder)
C # quick development of Windows service programs 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)
C # quick development of Windows service programs 15. Right-click "my computer" on the desktop and select "manage" to access the computer management console.
C # quick development of Windows service programs 16. In the "services" section of "services and applications", you can find that your Windows service is 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.
C # Notes for quick development of Windows service programs:
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
C # quick development of Windows service programs will be introduced here. I hope you can understand and learn how to develop C # Windows service programs quickly.
ASP. NET development technology exchange group:67511751(Recruitment ...)