Windows Service Development

Source: Internet
Author: User

Learn to teach and then know how to be sleepy.

It's hard to do without Windows service development. Then the net read some examples, add their own understanding, write down the development steps.

Development steps
    1. New Windows Service

    2. Add service, after publishing will be the service management of their own computers to see

    Partial class Myservice:servicebase {Timer timer1;        Public MyService () {InitializeComponent ();            } protected override void OnStart (string[] args) {//Todo:add code here to start your service. using (StreamWriter SW = File.appendtext (@ "D:\\log2.txt")) {SW.            WriteLine (DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss") + "Start.");            } timer1 = new Timer (); Timer1.            Interval = 2000; Timer1.                Elapsed + = (sender, e) + = {using (StreamWriter SW = File.appendtext (@ "D:\\log2.txt")) {SW.                WriteLine (DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss") + "Countue ...");            }            }; Timer1.        Start ();  } protected override void OnStop () {//Todo:add code here to perform any tear-down necessary to            Stop your service. using (StreamWriter SW = File.appendtext (@ "D:\\log2.txt")) {SW.            WriteLine (DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss") + "Stop."); }        }    }
    1. Add a service installer (this is a convenient operation for VS to provide) and can publish multiple services.

Right click, click Add Installer , Auto Generate class ProjectInstaller . You can see the VS auto-generated class ProjectInstaller.Designer.cs file

private void InitializeComponent()        {            this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();            this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();            //设置服务安装信息            this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService;            this.serviceProcessInstaller1.Password = null;            this.serviceProcessInstaller1.Username = null;            //要安装的服务(保证与你的服务名一致,可添加多个)            this.serviceInstaller1.ServiceName = "MyService";            //服务加入            this.Installers.AddRange(new System.Configuration.Install.Installer[] {            this.serviceProcessInstaller1,            this.serviceInstaller1});        }

Note Set the service installation to: install manually

    1. Service Security Loading scripts
      Install.bat file:

      %SystemRoot%\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe "%~dp0%WindowsServiceTest.exe"pause

      Uninstall.bat file:

      %SystemRoot%\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe -u "%~dp0%WindowsServiceTest.exe"pause

Windows Service Development

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.