Create and use Windows Services

Source: Internet
Author: User

1. Introduction to Windows service concepts

 

Microsoft Windows Services (formerly known as NT Services) enable you to create your own Windows sessions for a long time

The executable application that runs. These services can be automatically started when the computer is started. They can be paused and restarted without displaying any user interface.

This makes the service very suitable for use on the server, or at any time, in order not to affect other users working on the same computer, it takes a long time to run

Function. You can also run the service in a security context different from the logon user's specific user account or default computer account.

 

2. Create a Windows Service

 

Next we will use vs2010 to create a windwos service. This Windows Service will simply write a diary to the log file at intervals.

Of course, you can use it to process your more complex business logic. Here we will mainly talk about the creation process and simple applications.

 

Create a project, a Windows template, and a Windows service, as shown in

 

 

After creation, a file named service1.cs is generated. This is our windows service file. I want to change its name here,

Change to houseservice. CS (this is mainly used in actual projects). Of course, you do not need to change it here. This is mainly your preference.

 

Now we switch the houseservice to the design mode and place a timer control from the toolbox.

 

The timer we use here is system. Timers is not usually displayed in the toolbox. You can right-click the toolbox and select an item, as shown in

 

 

 

Here I change timer1 to housetimer

 

Click housetimer and change interval to 60000 In the attribute. That is, the polling time is 1 minute. Select its event elapsed and double-click it to enter the event body.

Namespace houseonline. windowsservice <br/> {<br/> Public partial class houseservice: servicebase <br/> {<br/> Public houseservice () <br/>{< br/> initializecomponent (); <br/>}< br/> protected override void onstart (string [] ARGs) <br/>{< br/>}< br/> protected override void onstop () <br/>{< br/>}< br/> private void housetimer_elapsed (Object sender, system. timers. elapsedeventargs e) <br/>{< br/>}< br/>} 

 

Next, we will switch back to the design mode to add an event log, that is, we can observe the status of our services through the event viewer.

Add an EventLog in design mode. Here I will change the name to houselog. In the attribute, we will change the log type to the application type.

Enter "houseservice" in the source attribute and implement the following code in onstart and onstop:

Protected override void onstart (string [] ARGs) <br/>{< br/> houselog. writeentry ("houseservice start"); <br/>}< br/> protected override void onstop () <br/>{< br/> houselog. writeentry ("houseservice stopped"); <br/>}< br/> private void housetimer_elapsed (Object sender, system. timers. elapsedeventargs e) <br/>{< br/> houselog. writeentry (string. format ("info: {0}", datetime. now. tostring (); <br/>} 

 

 

To run this service, we also need to add another installer, switch to the design mode, right-click to select add installer

 

The projectinstaller. CS file and serviceprocessinstaller1 and serviceinstaller1 are generated.

 

Here I change to serviceprocessinstaller and serviceinstaller.

 

Now let's modify their attributes to control the service installation and startup options. Select serviceprocessinstaller in the project installer design view,

Select the "Account" attribute as LocalSystem to start the service with this account. If you want the service to be automatically started when the system starts, select the starttype attribute of serviceinstaller as automatic. If it is manually started, select manaul.

 

Ii. Installation of Windows Services

 

Now we have developed our project. First, we will generate a houseservice.exe file in debug, but how can we make it run,

Now we need to install the service. The program is located in C:/Windows/Microsoft. NET/framework/v2.0.50727. click the Start menu and select "run". In the run dialog box, Enter cmd to enter the command line window,

Enter CD:/Windows/Microsoft. net/framework/v2.0.50727, enter this directory, and then enter installutil E:/VS2010-project/houseonline. windowsservice/bin/debug/houseservice.exe, the content behind installutil is ours

The path of the executable program generated by the project, which can be modified as needed. Delete a service

Installutil-u e:/VS2010-project/houseonline. windowsservice/bin/debug/houseonline.windowsservice.exe

 

The other method does not require the tool to be executed directly in cmd. That is

 

 

SC Delete houseservice

SC create houseservice binpath = "E:/VS2010-project/houseonline. windowsservice/bin/debug/houseonline.windowsservice.exe" type = share start = auto displayname = "houseservice" depend = TCPIP

 

 

As shown in figure

 

 

 

Iii. Windows Service debugging

 

 

1. Set the breakpoint. In general, we need another thread to execute the task in our service. You need to set the breakpoint in the thread's Execution Code.

2. Install the service. We will introduce how to install the service.

3. if your service start type is manual, you need to start your service in "service. in general, if your service is in the development stage, I recommend that you set the Service Startup type to manual to facilitate debugging, because if the service is running, you will not be able to build the project.

4. In vs, select debug-> attach process… from the menu ...., :

 

 

 

 

 

In the final running result, one log is written every minute in the event viewer,

 

 

 

Finally, delete the service cmd> SC Delete houseservice.

 

This is just the most basic Windows creation, use, installation, and debugging. You can make changes based on your business needs.

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.