About. A brief introduction to the net Background Timer service Framework

Source: Internet
Author: User
Background services as long as there is a certain experience of developers have been exposed, which can not be separated from service creation, scheduling logic processing, business logic and other aspects of writing. Often when we create a new background service project will copy the previous code, and then write some threads and other ways to complete, and then to deal with the installation of the service problem. Most of the time is wasted on these repetitive tasks.
Here is a simple background service processing framework, simple background service processing box support (at a specified interval of time execution; Specify time per day to execute once a day; Specify the start and end times per day and execute at a specified interval), when developing a new scheduled service task Just implement the methods of the abstract class, add the task configuration, and run the installation script to complete the development of a service.

Framework code path: framework Code


The framework supports configuring execution types to control the execution logic of tasks
Executes at a specified interval of time
Daily execution time per day
Specify time to execute once
Specify start and end times per day and execute at specified intervals

  

The following is a task that periodically executes a stored procedure.

Inheriting base classes and writing business logic code


Using dataaccesshelper.sqlhelper;using services.common;using system;namespace services.tasks{Public    class Callproctask:servicebase    {        protected override void Exec ()        {            try            {                if (_isstart)                {                    if (!string. Isnullorwhitespace (Config.param))                    {                        Logfactory.getlogger (). Info (String. Format ("Start execution of stored procedure {0}", Config.param));                        SQLHelperFactory.Instance.ExecuteNonQuery (Config.param, null);                        Logfactory.getlogger (). Info (String. Format ("Execute stored procedure {0} completed", Config.param));            }}} catch (Exception ex)            {                Logfactory.getlogger (). Error (String. Format ("Execute stored procedure {0} exception: {1}", Config.param, Ex));}}}    

Configure Service Name

  

Configure 60 seconds per execution interval


[  {//Loop execution 60 seconds per execution interval    "ServiceName": "Callproctask-proc_test Task",//Service name non-empty    "Assembly": "Services.exe",/ /assembly non-null    "Methods": "Services.Tasks.CallProcTask",//class name that executes the class name is  not empty    "s_interval": 60,//interval unit seconds    Exectype ": 0,//Execution Type (0: Execution at a specified interval of 1: Daily specified time is performed once per day 2: Specify time to execute once 3. Specify start and end times per day and execute at specified intervals) nullable default 0    " Param ":" Proc_test "/ /custom parameter In this case is a SQL parameter nullable  }]

Once written, compile, run: Install.bat to see the corresponding service in Service Manager.

Framework code path: framework Code

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.