C # Writing Windows Service Program Graphics Tutorial (reprint)

Source: Internet
Author: User

This piece of Windows service is not complicated, but there are too many things to notice, the online material is also very messy, and occasionally write their own will be absent-minded. So this article is also produced, this article will not write complex things, completely based on the requirements of the application to write, so will not write to the Windows service very deep.

This article describes how to create, install, start, monitor, and uninstall the content steps and considerations for a simple Windows Service in C #.

One, create a Windows Service

1) Create a Windows service project

2) Renaming the service

Rename Service1 to your service name, where we are named Servicetest.

Second, create the service installation program

1) Add Installer

We can then see that the ProjectInstaller.cs and 2 installed components are created automatically for us.

2) Modify the installation service name

Right-ServiceInsraller1, select Properties, and change the value of ServiceName to servicetest.

3) Modify Installation permissions

Right-ServiceProcessInsraller1, select Properties, and change the account value to LocalSystem.

Third, write the service code

1) Open Servicetest Code

Right-click servicetest and select View Code.

2) Write service logic

Add the following code:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Diagnostics;usingSystem.Linq;usingsystem.serviceprocess;usingSystem.Text;namespacewindowsservicetest{ Public Partial classServicetest:servicebase { PublicServicetest () {InitializeComponent (); }        protected Override voidOnStart (string[] args) {            using(System.IO.StreamWriter SW =NewSystem.IO.StreamWriter ("C:\\log.txt",true) ) {SW. WriteLine (DateTime.Now.ToString ("YYYY-MM-DD HH:mm:ss") +"Start."); }        }        protected Override voidOnStop () {using(System.IO.StreamWriter SW =NewSystem.IO.StreamWriter ("C:\\log.txt",true) ) {SW. WriteLine (DateTime.Now.ToString ("YYYY-MM-DD HH:mm:ss") +"Stop."); }        }    }}

Here our logic is very simple, start the service to write a log, close the time to write a log.

Iv. Creating the installation script

Add 2 files to the project as follows (must be ANSI or UTF-8 without BOM format):

1) Installation Script Install.bat

%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe windowsservicetest.exenet Start SERVICETESTSC Config servicetest start= Auto

2) Uninstall Script Uninstall.bat

%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe/u WindowsServiceTest.exe

3) Installation Script description

The second behavior starts the service.

The third behavior sets the service to run automatically.

These 2 lines are selected on a service basis.

4) Script debugging

If you need to see the script health, add pause to the last line of the script

V. Control of services in C #

0) Configuring the directory structure

CV a new WPF project, called Windowsservicetestui, adds a reference to the system.serviceprocess.

Create the service directory under the Windowsservicetestui bin\debug directory.

Set the build directory for windowsservicetest to the service directory created above.

Post-build directory structure such as

1) Installation

The installation will cause directory problems, so the installation code is as follows:

string CurrentDirectory ="\\Service"newfalse  "install.bat"true= CurrentDirectory;

2) Uninstall

A directory problem also occurs when uninstalling, so the uninstall code is as follows:

string CurrentDirectory ="\\Service"newfalse  "uninstall.bat"true= CurrentDirectory;

3) Start

The code is as follows:

Usingnew ServiceController ("servicetest"); Servicecontroller.start ();

4) Stop

New ServiceController ("servicetest"); if (servicecontroller.canstop)    servicecontroller.stop ();

5) Pause/Resume

New ServiceController ("servicetest"); if (servicecontroller.canpauseandcontinue) {    if (servicecontroller.status = =  servicecontrollerstatus.running)        servicecontroller.pause ();     Else if (Servicecontroller.status = = servicecontrollerstatus.paused)        servicecontroller.continue ();}

6) Check the status

New ServiceController ("servicetest"); string Status = ServiceController.Status.ToString ();

Vi. Debugging Windows Service

1) Install and run the service

2) Additional Process

3) Add breakpoints in your code to debug

Vii. Summary

The above configuration for Windows service is not explained in detail in this article, but as described above, you can create a running Windows service that meets the needs of your work.

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.