C # Write a Windows Service Program

Source: Internet
Author: User

Use C # To write Windows Services todayProgramWrite, write, and query information. There is an article on the Internet.ArticleWell written

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.

next 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 of 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 the Code of some database operations, for example,
sqlconnection conn = new sqlconnection ("Server = 127.0.0.1; database = test; uid = sa; Pwd = 275280 ");
sqlcommand comm =-New sqlcommand ("insert into tb1 ('000000', 11)", Conn);
Conn. open ();
comm. executenonquery ();
Conn. close ();
8. switch this service program to the design view
9. right-click the design view and select "add installer".

from: http://post.blog.hexun.com/imagine18/trackback.aspx? ArticleID = 6056177
10. switch to the design view of the newly added projectinstaller
11. set the properties of the serviceinstaller1 component:
1) servicename = My Sample Service
2) starttype = Automatic startup
12. set the attribute account = LocalSystem of serviceprocessinstaller1
13. change the path to the bin \ debug folder where your project is located (if you compile in release mode, go to the bin \ release folder)
14. run the "installutil mywindowsservice.exe" command to register this service and create an appropriate registration item. (Installutil this program 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.
(installutil/u mywindowsservice.exe deletes the Service)

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.