) How to Implement Windows Services

Source: Internet
Author: User
I. What problems have you encountered?
Some software must do the same thing at a certain time, or be used as a network service, such as IIS, SQL Server, and so on. In this case, the software is often required to run on the server without user intervention. And Program Interaction is often performed through network protocols. Or the program does not need to interact with the user at all. Such programs are often called backend programs or backend services.

2. What Can Windows do?
Windows service is a formal term for these background programs and background services. Windows Services can be run in the background without user intervention, without any interface. Use the Windows Service Manager for management. The Service Manager can only do some simple operations: start, pause, continue, stop.
Windows Services and common Windows Forms Application types, but without the interface, even the simplest MessageBox cannot pop up. Therefore, do not try to prompt users in the Windows service in this way. Because there may be no user logging on to the computer at all.
Features of Windows Services:
Run in the background
No user interaction
Can be started with Windows
3. How to Implement Windows Services?
The following describes how to implement the Windows service by taking the "do something the same after a certain period of time" as an example.
First, design your program logic according to the common Windows program.
Create a blank solution windowsservice. sln
Add servicebusiness. csproj to the Windows Class Library Project
Rename class1.cs to servicebusiness. CS
Add a method dothings (), which is used to call the method once and perform periodic tasks at intervals.

Using System;

namespace servicebusiness
{< br> Public class servicebusiness
{< br> Public void dothings ()
{< br> /// call once every other time
}< BR >}< br>

Add a windowsservice. csproj TO THE SOLUTION

Rename service1.cs as service. CS
Add servicebusiness project reference to windowsservice
Open service. CSCodeView to add members to the service class
Servicebusiness. servicebusiness;
Instantiate servicebusiness In the constructor
Servicebusiness = new servicebusiness. servicebusiness ();
Add system. theading at the using location
Using system. Threading;
Add a timer to the service class
Timer servicetimer;
Added the timecallback Method for timer call.

Public   Void Timercallback ( Object OBJ)
{
// Called every other time
Servicebusiness. dothings ();
}

Add a method to the onstart () method to start the timer.
Servicetimer = new timer (New timercallback (timercallback), State, 0, period );
Here, state is used to save the state. If you do not need to save the state, you can pass in null. The third parameter 0 indicates that the timercallback method is called immediately. If you do not need to call the method immediately, you can pass in the period. The period is the timer interval in milliseconds.
The onpause () and oncontinue () methods are reloaded to control the timer.
The service. CS code is as follows:

Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. diagnostics;
Using System. serviceprocess;
Using System. text;
Using System. Threading;

Namespace Windows Service
{
Public Partial Class Service: servicebase
{
Timer servicetimer;
Servicebusiness. servicebusiness;
Int Period;
Object State;
Public Service ()
{
Initializecomponent ();
Servicebusiness =   New Servicebusiness. servicebusiness ();
}

Protected   Override   Void Onstart ( String [] ARGs)
{
// Start Timer
Period = Servicesettings. Default. servicetimerintervalsecond *   1000 ;
Servicetimer =   New Timer ( New Timercallback (timercallback), state, 0 , Period );
}

protected override void onstop ()
{< br> /// stop timer
servicetimer. change (timeout. infinite, timeout. infinite);
}

Protected Override VoidOncontinue ()
{
//Start timing again
Servicetimer. Change (0, Period );
}

Protected   Override   Void Onpause ()
{
// Stop Timer
Servicetimer. Change (timeout. Infinite, timeout. Infinite );
}

Public   Void Timercallback ( Object OBJ)
{
// Called every other time
Servicebusiness. dothings ();
}

}
}

Open the program. CS File
To facilitate debugging, add the dothings () call directly in the main method (). In this way, you can easily debug the program in the IDE.

Using System. Collections. Generic;
Using System. serviceprocess;
Using System. text;

Namespace Windows Service
{
Static   Class Program
{
///   <Summary>
/// The main entry point of the application.
///   </Summary>
Static   Void Main ( String [] ARGs)
{
# If Debug
Servicebusiness. servicebusiness =   New Servicebusiness. servicebusiness ();
// Directly call
Servicebusiness. dothings (ARGs );
# Else
Servicebase [] servicestorun =   New Servicebase [] { New Service ()};
Servicebase. Run (servicestorun );
# Endif
}
}
}

Author: Lightning
From: baiguli.cnblogs.com

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.