Creation and debugging of C # Windows services

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 #.

First, create a Windows Service

1 ) to create Windows Service Project

2 ) to Service Renaming

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

Second, create the service installation program

1 ) Add the Setup program

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:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Diagnostics;6 usingSystem.Linq;7 usingsystem.serviceprocess;8 usingSystem.Text;9 namespacewindowsservicetestTen { One  Public Partial classServicetest:servicebase A { -  Publicservicetest () - { the 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."); A } at } - 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."); in } - } to } +}

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 ) Unload 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

Five, in C # control of the service in the

0 ) Configure 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:

1 stringCurrentDirectory =System.Environment.CurrentDirectory;2System.Environment.CurrentDirectory = CurrentDirectory +"\\Service";3Process Process =NewProcess ();4Process. Startinfo.useshellexecute =false;5Process. Startinfo.filename ="Install.bat";6Process. Startinfo.createnowindow =true;7 process. Start ();8System.Environment.CurrentDirectory = CurrentDirectory;

2 ) Uninstall

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

1 stringCurrentDirectory =System.Environment.CurrentDirectory;2System.Environment.CurrentDirectory = CurrentDirectory +"\\Service";3Process Process =NewProcess ();4Process. Startinfo.useshellexecute =false;5Process. Startinfo.filename ="Uninstall.bat";6Process. Startinfo.createnowindow =true;7 process. Start ();8System.Environment.CurrentDirectory = CurrentDirectory;

3 ) Start

The code is as follows:

1 using system.serviceprocess; 2 New ServiceController ("servicetest"); 3 servicecontroller.start ();

4 ) Stop

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

5 ) Pause / continue to

1ServiceController ServiceController =NewServiceController ("servicetest");2 if(servicecontroller.canpauseandcontinue)3 {4 if(Servicecontroller.status = =servicecontrollerstatus.running)5 servicecontroller.pause ();6 Else if(Servicecontroller.status = =servicecontrollerstatus.paused)7 servicecontroller.continue ();8}

6 ) Check Status

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

Six, commissioning Windows Service

1 ) Install and run the service

2 ) Attach Process

3 ) to debug by adding breakpoints in your code

Creation and debugging of C # Windows services (GO)

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.