C # Create and install a Windows service program

Source: Internet
Author: User

1. Create a new Windows service program.


2, click on this service class, add a Timer control from the toolbox, right the timer control named Timerorderdeductiondetailjob,enable set to True.



3. Right-click this service class to set the Timer control's start interval in the OnStart method.

        protected override void OnStart (string[] args)        {            //TODO:  Add code here to start the service.            this.timerOrderDeductionDetailJob.Interval = +;        }

4. Double-click the timer time control to register an event

       private void Timerorderdeductiondetailservice_elapsed (object sender, System.Timers.ElapsedEventArgs e) {      W Orking ();//method to execute}

5, the method of implementation

       private void Working () {New Exceptionhelper (). Writelog ("Start orderdeductiondetailjob Job" + DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss"), @ "            Orderdeductiondetailjob ");            this.timerOrderDeductionDetailJob.Enabled = false;                try {orderdeductiondetailjob Order = new Orderdeductiondetailjob ();            Order.execute ();                } catch (Exception ex) {StringBuilder sberror = new StringBuilder ();                Sberror.append ("Error time:" + DateTime.Now.ToString ("Yyyy-mm-dd hh:mm:ss") + Environment.NewLine); Sberror.append ("Error file:" + ex.)                Source + Environment.NewLine); Sberror.append ("error message:" + ex.)                Message + Environment.NewLine); Sberror.append ("method to raise the error:" + ex.)                TargetSite + Environment.NewLine); Sberror.append ("The trace that caused the error:" + ex.)                StackTrace + Environment.NewLine); New Exceptionhelper (). Writelog (sberror.tOstring (), @ "Orderdeductiondetailjob");            } this.timerOrderDeductionDetailJob.Enabled = true; New Exceptionhelper (). Writelog ("End orderdeductiondetailjob Job" + DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss"), @ "Orderdeductiondetailjob            "); New Exceptionhelper ().            Writelog ("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *" + Environment.NewLine + Environment.NewLine + ENVIRONMENT.N        Ewline, @ "Orderdeductiondetailjob"); }

6, add the service installation program, click on the service class, the right to add the installation program.


7. Click on the Installer class.


8, change the installation service name, StartType changed to Automatic.


9, ServiceProcessInstaller1 Right-click Property account changed to LocalSystem.


10, compile this service, install. There are two types of installation methods:

(1), windows+r (Run command prompt), input cmd

Run command: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe (space) window item bin\release under. exe file address

Example: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe (space) F:\chenfeilong\Project\Aidaiji Awin\adj. Job.orderdeductiondetailservice\bin\release\adj. Job.OrderDeductionDetailService.exe

(2), SC Create service name binpath= (space) "Windows Service item bin\release under. exe file Address" Start=auto


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.