[Reprint] C # Write a Windows Service Program

Source: Internet
Author: User

* This Article is not original to the blogger. The original author's article link is not saved in the favorites folder. Sorry, the copyright belongs to the original author. This document is only slightly modified.

1. Create a new C # Windows service: windows service Project

OnStart (

3. After the C # Windows service is created, set the service running cycle. There are two timmer in the ToolBox on the left, one under the component and the other under the windows form, unfortunately, neither of them can be used. We need to manually create a timmer and set its attributes and events.

=  System.Timers.Timer();    t.Elapsed +=  System.Timers.ElapsedEventHandler(TimeElapse);    t.AutoReset = ;    t.Enabled = ;=  FileStream(=  + DateTime.Now.ToString() + 

4. After the service is compiled, the SCM (Service Control Manager) cannot manage the service. You need to add a loader to the service. In the design view of Service1.cs, right-click and select "add loader". By default, the project Installer. cs class is added.

5. After adding the class, you can see serviceInstaller1 and serviceProcessInstaller1 in the design view of the class and set their attributes respectively.

Set the running mode of serviceInstaller1 to manual or automatic

Set the ServiceName of serviceInstaller1, set why, what is displayed in the service list

Set the Running Account of serviceProcessInstaller1 to LocalSystem

6. compile the project

7. Use the command line tool provided by vs to run the exe compiled by installutil.

8. You can see the service we created in the system service.

Note that:

And then reinstall the service. In addition, the C # 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.