1.Windows Service-Add a simple service

Source: Internet
Author: User

A Windows Service application is an application that needs to run for a long time and is particularly suitable for a server environment. It does not have a user interface and does not produce any visual output. Any user messages will be written into the Windows event log. When the computer starts, the service automatically starts running. They don't have to be logged in to run, they can run in any user environment that includes the system. Through the Service Control Manager, Windows services are controllable and can be terminated, paused, and started when needed.

1. Create a new Windows Service project

  

2. The system automatically creates a Service1.cs file, double-click Open Service1.cs file, right-click on the work area, or F4 to view the file properties

  

  

  

Autolog whether to automatically write to the system's log files

Canhandlepowerevent receive power events during service

Whether the CanPauseAndContinue service accepts requests that are paused or continue to run

Whether the CanShutdown service receives a notification when the computer on which it is running is closed so that the OnShutdown procedure can be called

Whether the CanStop service accepts a request to stop running

ServiceName Service Name

3. The amount, The default Service1.cs file name is really not pleasing, so we changed the file name to Testservice (note that the file name, not servicename, in the previous step, I have defined servicename as testservice, of course, can be other)

  

The main function of the 4.Windows service is written in the program file, so let's look at its code and find out if we define n multiple services, we can define which service to run.

        /// <summary>        /// The main entry point for the application.         /// </summary>        Static void Main ()        {            servicebase[] servicestorun;             New servicebase[]             {                 new  testservice ()             };            Servicebase.run (ServicesToRun);        }
View Code

5. View Testservice code, by default implements OnStart and OnStop two methods to write data to a text as an example

         PublicTestservice () {InitializeComponent (); }        /// <summary>        ///service started./// </summary>        protected Override voidOnStart (string[] args) {HGL. Toolkit.IOHelper.FileOperate.WriteFile (@"F:\1.txt","the service started \ n"); }        /// <summary>        ///Service stopped./// </summary>        protected Override voidOnStop () {HGL. Toolkit.IOHelper.FileOperate.WriteFile (@"F:\1.txt","the service stopped. \ n"); }        /// <summary>        ///system shutdown/// </summary>        protected Override voidOnShutdown () {HGL. Toolkit.IOHelper.FileOperate.WriteFile (@"F:\1.txt","system off \ n"); }        /// <summary>        ///the service is suspended./// </summary>        protected Override voidOnPause () {HGL. Toolkit.IOHelper.FileOperate.WriteFile (@"F:\1.txt","the service is paused \ n"); }        /// <summary>        ///Service continues/// </summary>        protected Override voidOnContinue () {Base.        OnContinue (); }        /// <summary>        ///system power State Change/// </summary>        protected Override BOOLonpowerevent (PowerBroadcastStatus powerstatus) {return Base.        Onpowerevent (Powerstatus); }
View Code

6. Install the service, switch to the service1.cs[design] screen, right-click to select "Add Installer".

A new class ProjectInstaller and two installation components ServiceProcessInstaller and ServiceInstaller are added to the project.

Select the ServiceInstaller1 control, F4 open the Properties panel

   

Description of the Description service program

DisplayName The name that the service program displays

StartType specifying how to start a service

After the Manual service is installed, it must be started manually

Automatic every time the computer restarts, the service will start automatically

Disabled Service failed to start

Select the ServiceProcessInstaller1 control, F4 open the Properties panel

  

Change the Account property of the ServiceProcessInstaller class to LocalSystem. This way, the service will always start, regardless of which user is logged on to the system.

7. Right-click the project to select Build, Windows service cannot run through F5

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.