C # compile a Windows Service program ),

Source: Internet
Author: User

C # compile a Windows Service program ),

  •  Windows Service Introduction:

A Windows Service Program is an executable application that can complete specific functions in a Windows operating system. Although the Windows service program is executable, it is not like a normal executable file and can be run by double-clicking,It must have a specific startup method. These startup methods include automatic start and Manual start.. For automatically started Windows service programs, they are executed before the user logs on after Windows is started or restarted. You only need to register the corresponding Windows Service Program in Service Control Manager and set the startup category to Automatic startup. For Windows service programs that are manually started, you can use the command line toolNET STARTCommand to start it, or start the corresponding Windows service program through a service item under the management tool in the control panel.

Similarly, a Windows service program cannot be terminated as a normal application. Because Windows service programs generally do not have a user interface, you need to stop them using the command line tool or the tool shown in the figure below, or make the Windows Service Program stop automatically when the system is disabled. Because Windows service programs do not have a user interface, the user interface-based API functions have little significance for them. To enable a Windows service program to work normally and effectively in a system environment, programmers must implement a series of methods to complete its service functions. Windows service programs have a wide range of applications. Typical Windows service programs include hardware control, application monitoring, system-level applications, diagnostics, reporting, Web and file system services.

Namespaces related to Windows Services involve the following:System. ServiceProcessAndSystem. Diagnostics.

  • Use C # To create a Windows Service:

1. Create a Windows Service Project

Select the Windows Service option from the Visual C # project, give the project a new file name, and click OK.

2. Implement functions in the service

The OnStart function is executed when the service is started, and the OnStop function is executed when the service is stopped. Here, when the service is started and stopped, some text information is written to a text file. The Code is as follows:

 

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Diagnostics;
Using System. IO;
Using System. Linq;
Using System. ServiceProcess;
Using System. Text;
Using System. Threading. Tasks;

Namespace MyService
{
Public partial class Service1: ServiceBase
{
Public Service1 ()
{
InitializeComponent ();
}

Protected override void OnStart (string [] args)
{
FileStream fs = new FileStream (@ "d: \ xx.txt", FileMode. OpenOrCreate, FileAccess. Write );
StreamWriter sw = new StreamWriter (fs );
Sw. BaseStream. Seek (0, SeekOrigin. End );
Sw. WriteLine ("WindowsService: Service Started" + DateTime. Now. ToString () + "\ n ");

Sw. Flush ();
Sw. Close ();
Fs. Close ();
}

// Protected override void OnContinue ()
//{
// Base. OnContinue ();
//}

// Protected override void OnPause ()
//{
// Base. OnPause (); // father class method inherit
//}

// Protected override void OnShutdown ()
//{
// Base. OnShutdown ();
//}

Protected override void OnStop ()
{
FileStream fs = new FileStream (@ "d: \ xx.txt", FileMode. OpenOrCreate, FileAccess. Write );
StreamWriter sw = new StreamWriter (fs );
Sw. BaseStream. Seek (0, SeekOrigin. End );
Sw. WriteLine ("WindowsService: Service Stopped" + DateTime. Now. ToString () + "\ n ");
Sw. Flush ();
Sw. Close ();
Fs. Close ();

}
}
}

 

4. Return to the design window and right-click and choose-Add installer-Generate two components: serviceInstaller1 and serviceProcessInstaller1.
Change the ServiceName attribute of serviceInstaller1 to your service program name, and set the startup mode to AUTOMATIC
Rewrite the attribute account of serviceProcessInstaller1 to LocalSystem


5. Compile the link to generate a service program

You can select generate from the generate menu to generate a project.

 

6. Install the service

Use the. net framework tool INSTALLUTIL to install the service program.

Use the project output as a parameter to run InstallUtil.exe from the command line. Enter the following code in the command line:
Installutil yourproject.exe

Hint: a windows service must first be installed using installutil.exe and then started with the serviceExplorer, windows Services Administrative tool or the net start command.

 

7. Uninstall the service

Use the project output as a parameter to run InstallUtil.exe from the command line.

Installutil/u yourproject.exe

 

The preceding program running result is as follows:

 

  • Supplement:

1. Service Startup attributes:

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

The service is automatically started every time the computer restarts.

The Disabled service cannot be started.

 

2. The meaning of each attribute in the newly created Service Project (design View> right-click attribute ):

Whether Autolog is automatically written to the System Log File

Receive Power Events During CanHandlePowerEvent Service

Whether the CanPauseAndContinue Service accepts the pause or continue running request

Whether the CanShutdown Service receives a notification when the computer that runs it shuts down, so that it can call the OnShutDown Process

Whether the CanStop Service accepts the stop operation request

ServiceName service name

 

3. You can also set the properties or startup method of the corresponding Service in the System Service Manager.

Computer Management-> services and applications-> services->...

 

 

This article Reprinted from http://www.cnblogs.com/bluestorm/p/3510398.html

Related Article

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.