Create a scheduled window service task

Source: Internet
Author: User

References:Http://www.cnblogs.com/jack-liang/archive/2011/05/20/2051743.html

A project was created some time ago. The front-end system provides the function of adding scheduled tasks. The back-end system always scans database tasks and performs related operations. For the backend system, the first thing that comes to mind is to create a scheduled task in the window service. So I took a look at some information on the Internet and successfully completed the task. The procedure for creating a window service is recorded for future review.

1Open vs2008/vs2010 and create the window service project mywindowservice.

2, Add window service item myservice. CS, the Code is as follows:

View code

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. diagnostics;
Using system. LINQ;
Using system. serviceprocess;
Using system. text;

Namespace mywindowservice
{
Public partial class myservice: servicebase
{
System. Timers. Timer time = NULL; // Timer

Public myservice ()
{
Initializecomponent (); // set the service name in this method: This. servicename = "myservice ";

Time = new system. Timers. Timer ();

System. Collections. Specialized. namevaluecollection appsettings = system. configuration. configurationmanager. receivettings; // obtain the configuration file information

Int interval = 300000;

If (maid ["interval"]! = NULL)
{
If (! Int. tryparse (Fig ["interval"]. tostring (), out interval ))
{
Interval = 300000; // The default value is 5 minutes.
}
}

If (interval <60000) // The minimum interval is 1 minute.
Interval = 6000;
If (interval> 3600000) // The maximum interval is 1 hour.
Interval = 3600000;

Time. interval = interval; // set the interval at which elapsed events are triggered, in milliseconds.

Time. autoreset = true;

Time. Enabled = false; // indicates that the elapsed event is triggered.

Time. elapsed + = new system. Timers. elapsedeventhandler (time_elapsed );

// Trigger an event at a scheduled time. If the event is not triggered at a scheduled time but is always running, use protected override void onstart (string [] ARGs) and protected override void onstop () two methods are used to indicate the start and end events.

Time. Start ();
}

// Scheduled trigger event
Private void time_elapsed (Object sender, system. Timers. elapsedeventargs E)
{
// Note: The elapsed event of timer is triggered by a new thread.
// To avoid problems that may occur when multiple threads run simultaneously, you can: 1. Set the interval as long as possible; 2. Add the following code time at the beginning and end of the method. enabled = false; and time. enabled = true;

// To do
}

Protected override void onstart (string [] ARGs)
{
// Set to trigger the elapsed event
Time. Enabled = true;
}

Protected override void onstop ()
{
// Set to cancel the elapsed event
Time. Enabled = false;
}
}
}

3, Add the window service installer class myserviceinstaller. CS.

Method 1: Add a class myserviceinstaller to inherit the installer

The Code is as follows:

 

View code

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. serviceprocess;
Using system. configuration. Install;
Using system. componentmodel;

Namespace sinaweiboservice
{
[Runinstaller (true)]
Public class myserviceinstaller: Installer
{
Serviceprocessinstaller processinstall;
Serviceinstaller serviceinstall;
Public sinaserviceinstall ()
{
This. processinstall = new serviceprocessinstaller ();
This. serviceinstall = new serviceinstaller ();
// This. serviceinstall. starttype = servicestartmode. Automatic;

// Set starttype to automatic)
Processinstall. Account = serviceaccount. LocalSystem;
This. serviceinstall. servicename = "myservice ";
This. installers. Add (this. serviceinstall );
This. installers. Add (this. processinstall );

// Or use the Add Set Method

// This. installers. addrange (new system. configuration. Install. installer [] {serviceinstall, processinstall });
}

}
}

Method 2:

Open the myservice. CS design view, right-click and choose "add installer". The program will create the installer class for myservice by default.

Set the parameters in the initializecomponent () method.

4The program. CS file code is as follows:

View code

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. serviceprocess;
Using system. text;

Namespace mywindowservice
{
Static class Program
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
Static void main ()
{
Servicebase [] servicestorun;
Servicestorun = new servicebase []
{
New myservice ()
};
Servicebase. Run (servicestorun );
}
}
}

 

5After the project is compiled successfully, open the vs command tool and register and uninstall the service in the system.

Registration Service:

C: \ windows \ Microsoft. NET \ framework \ v2.0.50727 \ installutil mywindowservice.exe

Uninstall the service:

C: \ windows \ microsoft. Net \ framework \ v2.0.50727 \ installutil-u mywindowservice.exe

You can put the above two lines of code in a cmd or bat type file, so that you can double-click it.

After registering the service, you can see the service you just created.

 

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.