Simple job in C #. Net

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Threading;usingsystem.timers;namespacetestjobs{classProgram {Private Static ReadOnlyCronmon Cronmon =NewCronmon (); Static voidMain (string[] args)            {Start ();        Console.readkey (); }         Public Static voidStart () {Cronmon.addjob (NewJob ()); Cronmon.addjob (NewJob (Ten)); Cronmon.addjob (NewJob (5));        Cronmon.start (); }    }    classJob:cronjobbase {Private int_span =0;  PublicJob () {} PublicJob (intspan):Base(span) { This. _span =span;} protected Override voidExecute () {if(_span = =0) Console.WriteLine ("I am the job, I use the default time to execute!"+datetime.now.tostring ("s")); ElseConsole.WriteLine ("I am job, I interval {0}s execute! {1}", _span, DateTime.Now.ToString ("s")); }     }    InterfaceIcronjob {voidRun (datetime datetime); voidAbort (); }    classCronjobbase:icronjob {Private ReadOnly Object_object =New Object(); PrivateThread _thread; PrivateDateTime _runtime =DateTime.Now; Private  int_span=3;//time interval 3s         Publiccronjobbase () {} PublicCronjobbase (intspan) {_span=span; }         Public voidRun (datetime datetime) {if(_thread! =NULL&& _thread. ThreadState = =threadstate.running)return; if(_runtime.addseconds (_span) >dateTime)return; _runtime=DateTime; _thread=NewThread (Executehandler); _thread.        Start (); }         Public voidAbort () {_thread.        Abort (); }        Private voidExecutehandler () {Try{Execute (); }            Catch(Exception ex) {Throw NewException (ex.            ToString ()); }        }        protected Virtual voidExecute () {}}InterfaceIcronmon {voidaddjob (icronjob job); voidStart (); voidStop (); }    classCronmon:icronmon {Private ReadOnlySystem.Timers.Timer _timer =NewSystem.Timers.Timer ( +); Private ReadOnlyList<icronjob> _jobs =NewList<icronjob>(); PrivateDateTime _last =DateTime.Now;  PublicCronmon () {_timer. AutoReset=true; _timer. Elapsed+=_timer_elapsed; }        Private void_timer_elapsed (Objectsender, Elapsedeventargs e) {            if(DateTime.Now.Second! =_last. Second) {_last=DateTime.Now; foreach(varIteminch_jobs) {Item.                Run (DateTime.Now); }            }        }         Public voidaddjob (icronjob job) {_jobs.        ADD (Job); }         Public voidStart () {_timer.        Start (); }         Public voidStop () {_timer.            Stop (); foreach(varIteminch_jobs) {Item.            Abort (); }        }    }}

Simple job in C #. Net

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.