First Windows Service

Source: Internet
Author: User

Windows service has its unique features and can play a role in many applications. For example, long polling is required.

Windows Services enable you to perform tasks that execute as different background processes. you can use Windows Services to perform tasks, such as monitoring the usage of a database. A Windows service executes in its own process space until a user stops it, or the computer is shut down.

Understanding several concepts and key classes before development: (for authoritative explanations, see msdn. Here is my understanding)

1. servicebase: basic service class. All services can be inherited and implemented as a Windows service.

You override the methods in the servicebase class to create a service application. The methods of this class enable you to specify the behavior of your service application.

Method

Onstart onpause onstop oncontinue onshutdown oncustomcommand onpowerevent)

Child classes can selectively override the main methods, such as onstart and onstop. Basically, you have to rewrite these two methods so that your class has its own personality.

2. Service Installation and service development:

It has been installed on the service for half a day. From the above we can see that service development is actually not very difficult (I mean the business is also very simple ). Then the service installation is very troublesome.

The system. configuration. Install. installer class is the basic class for installation. You can use it to add other installers for service installation, such as loginstaller and performancecounterinstaller.

This. installers. addrange (new system. configuration. Install. installer [] {
This. eventloginstaller1,
This. performancecounterinstaller1,
This. serviceprocessinstaller1,
This. serviceinstaller1 });

You canCodeYou can also drag and drop these components.

Serviceinstaller must specify servicename, which cannot be the same as the existing service.

If you have added eventloginstaller, you must be careful that its log and source attributes must be specified. For example, log = application, Souce = serviceinstall. servicename.

Because of this, I have been engaged for a long time :(

Continue ....

You need to add serviceprocessinstaller, but you need to pay attention to its account attributes. It is a user by default. This will pop up the dialog box when installing the service, requiring the user name and password. If it is not specified correctly, it is troublesome, and you will not think of the exception here. Exception information:

An exception occurred during the "installation" phase.
System. argumentoutofrangeexception: The index and length must reference the position in the string.
Parameter Name: Length

Set it to LocalSystem to work.

3. servicecontroller:

After specifying the service name, you can useProgramTo control your services. This brings some UI opportunities for unbounded services.

Basic concepts and technologies, go to development and use this demo to implement a simple mail sending function.

1) inherit servicebase to implement your own services. This process is very simple. vs2005 integrates service development items.

Partial class mailservice: servicebase {..}

The override onstart and onstop methods are required by default.

Protected override void onstart (string [] ARGs) {}----------- this parameter is quite meaningful. For example, if the email list is a file, the location can be pointed out here. The thread sleep time can also be provided here.

For example:

If (ARGs. length> 0)
{
Try
{
Sleep_time = convert. toint32 (ARGs [0]);
}
Catch (exception)
{
Console. writeline ("error ARGs! ");
}
}

Protected override void onstop (){}

2) install the service. If your business is simple, half of the work has been completed.

Service installation can also be completed by adding a new entry in vs2005. At least two installers must be added. One is serviceinstaller and serviceprocessinstaller.

You can also add eventloginstaller, performancecounterinstaller, and messagequeueinstaller.

Note that we have mentioned above.

3) The installation of your service is complete. The command is as follows: installerutil filename.exe

If everything goes well. You can see your service on SCM (service control management.

We can see from the above process. Windows Service itself is quite simple. So there is a reason to spend more time and energy on your business. Here is mail sending, so how to implement this function better and more easily becomes the primary issue.

Mail configuration: completed through the configuration file.

Mail sending: encapsulated SMTP mail sending class.

Email list: igetmaillist

Business: Determine whether to send the message as needed.

A. Configuration:

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<SMTP deliverymethod = "network" from = "flyingchen @ *****. com">
<Network host = "*****" Port = "25" defaultcredentials = "true" username = "" Password = "" enablessl = "false"/>
</SMTP>
<Subject>
<Content> Information </content>
</Subject>
<Body>
<Content>

More Info
</Content>
</Body>
</Configuration>

You can still change the configuration in the service process.

B. I don't need to say much about this.

C. There are many sources of the email list. These differences can be shielded through this interface if there is a file, a slave database, or an Exchange Server. The message sending class returned is ilist <string>

D: Public Delegate bool needsend (); defines such a delegate. Determines whether to send emails based on business needs.

So far. Test it. In a small attack, set 1 second to send a copy to a colleague's mailbox. Haha, let him go!

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.