Asp.net (C #) Windows Service {used to implement scheduled tasks, event monitoring, etc}

Source: Internet
Author: User
ArticleDirectory
    •  
    • The following describes how to use vs2010 (C #) to compile a Windows application (similar to vs2008 and 2005 ).

What is a Windows service?

A Windows ServiceProgramIt is an executable application that can complete specific functions in a Windows operating system. Although a Windows service program is executable, it does not run as a normal executable file by double-clicking it. It must have a specific startup method. These 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 a Windows service program that is manually started, you can use the net start command of the command line tool to start it, or start the corresponding Windows service program through the service item under the management tool in the control panel (see figure 1 ). 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, reports, web and file system services.

Because the project needs to use the Windows service to schedule tasks, similar functions are required.

And my other blogASP. NET scheduled tasks (implemented by. net internal mechanism without using external programs)A bit similar. It also uses the time control timing detection to execute tasks.

Not much nonsense:

Open Windows Service: computer desktop ==> Start Menu ==> program ==> management tools ==> Service {example}

The following describes how to use vs2010 (C #) to compile a Windows application (similar to vs2008 and 2005 ).

1. Create a Windows Service Project

Open the toolbar at the top of vs2010: file ==> new ==> Project

2. program description

{Such files will be available in the solution after the creation}

Double-click to open the design meeting;

Press F7 or right-click to viewCode;

Main Methods: for example

// Execute protected override void onstart (string [] ARGs) {}// execute protected override void onstop () when the service is stopped () {}// when the service is suspended, execute protected override void onpause () {}// when the service is resumed, execute protected override void oncontinue (){}

Service events corresponding to the above four events

Click a service.

The four buttons in the box correspond to the four methods of the above program. Don't say that you don't understand which button corresponds to which method. You don't know how to match the four buttons.

 

3. implement our scheduled tasks (here we will use the scheduled tasks as an example. You can also implement other functions such as monitoring based on Huludao ).

First, double-click service1.cs to enter the design interface and drag a time control.

Default attribute

But you want to add events

Add code for the event to implement the task scheduling function.

// This method runs one private void timer1_elapsed (Object sender, system. timers. elapsedeventargs e) {This. timer1.enabled = false; // Add the function code you need here. I want to add the check plan to execute this. timer1.enabled = true ;}

The time control method takes effect when you add a Service Startup event method.

 
Protected override void onstart (string [] ARGs) {// todo: Add code here to start the service. // Make the time control take effect and call the timerreceivelapsed () method this. timer1.interval = 10000; this. timer1.enabled = true ;}

Stop and pause. The recovery method is similar to controlling whether timer1 takes effect or becomes invalid;

Now the simple Windows service function has been completed. What are your feelings?

Haha, I think you can see that F5 is useless?

Yes, Windows Services cannot be debugged by pressing F5. You need to start the service and then attach the process.

Method 1:Many methods for starting services on the Internet, such as using

The simplified version of this method is write batch processing. If you do not understand it, you can contact it in private.

Method 2:

In service1.cs, go to the design page and right-click Add installer.

Is there an additional file [projectinstaller. CS]?

Double-click a file

First, the attributes in the serviceprocessinstaller1 box must be set as follows, mainly because of permission issues. You can set other attributes on your own.

Right-click the solution and add the installation project.

Set the output group on project output to set your own services as the primary output

Then generate the Service Project and installation project

Right-click the installation project and click "Install" to install the service.

If you have modified the service project content, regenerate the Service Project and installation project, and then click Install.

The service has been installed, and you need to go to computer service management to enable the Service

In this waySuccess!

Next, let's talk about debugging. debugging should be performed after the service is enabled.

Then, debug the program by appending the process.

Find your own Service APPEND

You can perform debugging.

Source codeThis is because the company's project code will not be uploaded.

Hope everyone can make progress together!

 

Because some details are not clearly written, some students do not understand it.

1. The service cannot be installed or uninstalled.

Add custom operations to the installer Project

Select your project file

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.