C # Create a Windows Service

Source: Internet
Author: User

When we need a program to run for a long time, but do not need to display the interface can consider using Windows service to implement. This blog will briefly explain how to create a Windows service, install/Uninstall Windows service.

To create a new Windows Service project:

Delete the auto-generated Service1.cs file, create a new Windowsservice class, and inherit the ServiceBase.

    Class Windowsservice:servicebase {public Windowsservice ()} {this.            ServiceName = "Test Windows Service"; This.            EventLog.Log = "Application"; This.            Canhandlepowerevent = true; This.            Canhandlesessionchangeevent = true; This.            CanPauseAndContinue = true; This.            CanShutdown = true; This.        CanStop = true; } #region//can place the requirement implementation code within the overridden method protected override void Dispose (bool disposing) {BA Se.        Dispose (disposing); } protected override void OnStart (string[] args) {base.        OnStart (args); } protected override void OnStop () {base.        OnStop (); } protected override void OnPause () {base.        OnPause (); } protected override void OnContinue () {base.        OnContinue (); } protected override void OnShutdown () {base. OnShutdown ();        } protected override void Oncustomcommand (int command) {base.        Oncustomcommand (command); } protected override bool Onpowerevent (PowerBroadcastStatus powerstatus) {return base.        Onpowerevent (Powerstatus); } protected override void Onsessionchange (SessionChangeDescription changedescription) {base.        Onsessionchange (changedescription); } #endregion}

Create a new Windowsserviceinstaller class, add a System.Configuration.Install reference,

    [Runinstaller (True)]    Class Windowsserviceinstaller:installer    {public        Windowsserviceinstaller ()        {            ServiceProcessInstaller ServiceProcessInstaller = new ServiceProcessInstaller ();            ServiceInstaller ServiceInstaller = new ServiceInstaller ();            Serviceprocessinstaller.account = Serviceaccount.localsystem;            Serviceprocessinstaller.username = null;            Serviceprocessinstaller.password = null;            Serviceinstaller.displayname = "CSharp Windows Service";            Serviceinstaller.starttype = servicestartmode.automatic;            Serviceinstaller.servicename = "Windows Service";            This. Installers.add (ServiceProcessInstaller);            This. Installers.add (ServiceInstaller);        }    }

Compile the project, at which point the compiled CSWindowsService.exe runs after the prompt:

We need to deploy Windows Service through Installutil.exe.

Run developer Command Prompt as Administrator. deployment Command: installutil/i file path .

Example: installutil/i D:\CSWindowsService.exe

Uninstall the command installutil/u file path.

Here, a simple Windows serive is finished.

For more information about Windows service, please refer to the MSDN documentation.

Thank you for reading, code click here to download.

C # Create a Windows 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.