My first Windows service

Source: Internet
Author: User

Code:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Diagnostics;usingSystem.IO;usingSystem.Linq;usingsystem.serviceprocess;usingSystem.Text;usingSystem.Threading.Tasks;namespacewindowsservice1{ Public Partial classService1:servicebase {//TimerSystem.Timers.Timer T =NULL;  PublicService1 () {InitializeComponent (); //Enable pause Recovery            Base. CanPauseAndContinue =true; //executes once every 5 secondst =NewSystem.Timers.Timer ( the); //Whether the setting is executed once (false) or always executed (true);T.autoreset =true; //whether to execute the System.Timers.Timer.Elapsed event;t.enabled =true; //time of arrival execution event (Theout method);t.elapsed + =NewSystem.Timers.ElapsedEventHandler (theout); }        protected Override voidOnStart (string[] args) {            stringState = DateTime.Now.ToString ("YYYY-MM-DD Hh:mm:ss") +"Start";          Writelog (state); }        protected Override voidOnStop () {stringState = DateTime.Now.ToString ("YYYY-MM-DD Hh:mm:ss") +"Stop";          Writelog (state); }        //Recovery Service Execution        protected Override voidOnContinue () {stringState = DateTime.Now.ToString ("YYYY-MM-DD Hh:mm:ss") +"continue to";            Writelog (state);        T.start (); }        //Pause Service Execution        protected Override voidOnPause () {stringState = DateTime.Now.ToString ("YYYY-MM-DD Hh:mm:ss") +"Pause";            Writelog (state);        T.stop (); }         Public voidWritelog (stringstr) {            using(StreamWriter SW = File.appendtext (@"D:\service.txt") ) {SW.                WriteLine (str); Sw.            Flush (); }        }         Public voidTheout (Objectsource, System.Timers.ElapsedEventArgs E) {Writelog ("theout:"+ DateTime.Now.ToString ("YYYY-MM-DD Hh:mm:ss")); }      }}

Open ProjectInstaller, modify ServiceInstaller1 Component Properties

description= My Service Memo service remarks description

Displayname= My service-friendly name

Servicename=myservice Install server name

Starttype=automatic Service Type

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

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

The Disabled service could not be started.

and design the properties of the ServiceProcessInstaller1 Account=localsystem

If the account is set to user, the installation will be:

Install the Windows service to have Installutil.exe, this is installed vs when the time has

Look at the system version and NET version of your Windows service to find Installutil.exe,

Mine is in: C:\Windows\Microsoft.NET\Framework\v4.0.30319

and copy it to the new service Bin/debug inside, is with the Windows service exe in a folder, so that the installation is better after

Installation

Dos to the folder where the Windows service resides

Installutil.exe WindowsService1.exe

After installation, you can view it in the service.

Unloading

Installutil.exe-u WindowsService.exe

Click Service to start

Error

Just set the Service Permissions tab to everyone.

View Output File

The third boot is a reboot, and the service first stops and then starts

http://blog.csdn.net/angle860123/article/details/17375895

My first Windows service

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.