C # scheduled task,

Source: Internet
Author: User

C # scheduled task,

In the work, you need to make a daily scheduled mail function. After learning about it, you will know that java has a relatively easy method for doing scheduled tasks, that is, Quartz. Net, which is called Quartz. Net in C.

Before writing code, you must reference several dll files, including C5.dll, Common. Logging. dll, and Quartz. dll. You can also copy the QuartzManager. cs file to the project.

QuartzManager. cs

Using System; using System. collections. generic; using System. linq; using System. text; using Quartz. impl; using Quartz. impl. triggers; namespace Quartz {public static class QuartzManager {private static ISchedulerFactory sf = null; private static IScheduler sched = null; static QuartzManager () {sf = new StdSchedulerFactory (); sched = sf. getScheduler (); sched. start ();} /// <summary> /// Add a Job and run it as a fixed point /// </summary> /// <typeparam name = "T"> </typeparam>/ // <param name = "JobName"> </param> // <param name = "CronTime"> </param> // <param name = "jobDataMap"> </param> // <returns> </returns> public static DateTimeOffset AddJob <T> (string JobName, string CronTime, string jobData) where T: IJob {IJobDetail jobCheck = JobBuilder. create <T> (). withIdentity (JobName, JobName + "_ Group "). usingJobData ("jobData", jobData ). build (); ICronTrigger CronTrigger = new CronTriggerImpl (JobName + "_ CronTrigger", JobName + "_ TriggerGroup", CronTime); return sched. scheduleJob (jobCheck, CronTrigger );} /// <summary> /// Add a Job and run it as a fixed point /// </summary> /// <typeparam name = "T"> </typeparam>/ // <param name = "JobName"> </param> // <param name = "CronTime"> </param> // <returns> </returns> public static DateTimeOffset AddJob <T> (string JobName, string CronTime) where T: IJob {return AddJob <T> (JobName, CronTime, null );} /// <summary> /// Add a Job and run it cyclically /// </summary> /// <typeparam name = "T"> </typeparam>/ // <param name = "JobName"> </param> // <param name = "SimpleTime"> millisecond count </param> /// <returns> </returns> public static DateTimeOffset AddJob <T> (string JobName, int SimpleTime) where T: IJob {return AddJob <T> (JobName, DateTime. utcNow. addMilliseconds (1), TimeSpan. fromMilliseconds (SimpleTime ));} /// <summary> /// Add a Job and run it cyclically /// </summary> /// <typeparam name = "T"> </typeparam>/ // <param name = "JobName"> </param> // <param name = "SimpleTime"> millisecond count </param> /// <returns> </returns> public static DateTimeOffset AddJob <T> (string JobName, dateTimeOffset StartTime, int SimpleTime) where T: IJob {return AddJob <T> (JobName, StartTime, TimeSpan. fromMilliseconds (SimpleTime ));} /// <summary> /// Add a Job and run it cyclically /// </summary> /// <typeparam name = "T"> </typeparam>/ // <param name = "JobName"> </param> // <param name = "SimpleTime"> </param> // <returns> </returns> public static DateTimeOffset AddJob <T> (string JobName, dateTimeOffset StartTime, TimeSpan SimpleTime) where T: IJob {return AddJob <T> (JobName, StartTime, SimpleTime, new Dictionary <string, object> ());} /// <summary> /// Add a Job and run it cyclically /// </summary> /// <typeparam name = "T"> </typeparam>/ // <param name = "JobName"> </param> // <param name = "StartTime"> </param> // <param name = "SimpleTime"> millisecond count </param> /// <param name = "jobDataMap"> </param> /// <returns> </returns> public static DateTimeOffset AddJob <T> (string jobName, dateTimeOffset StartTime, int SimpleTime, string MapKey, object MapValue) where T: IJob {Dictionary <string, object> map = new Dictionary <string, object> (); map. add (MapKey, MapValue); return AddJob <T> (JobName, StartTime, TimeSpan. fromMilliseconds (SimpleTime), map );} /// <summary> /// Add a Job and run it cyclically /// </summary> /// <typeparam name = "T"> </typeparam>/ // <param name = "JobName"> </param> // <param name = "StartTime"> </param> // <param name = "SimpleTime"> </param> /// <param name = "jobDataMap"> </param> /// <returns> </returns> public static DateTimeOffset AddJob <T> (string JobName, dateTimeOffset StartTime, TimeSpan SimpleTime, Dictionary <string, object> map) where T: IJob {IJobDetail jobCheck = JobBuilder. create <T> (). withIdentity (JobName, JobName + "_ Group "). build (); jobCheck. jobDataMap. putAll (map); ISimpleTrigger triggerCheck = new SimpleTriggerImpl (JobName + "_ SimpleTrigger", JobName + "_ TriggerGroup", StartTime, null, SimpleTriggerImpl. repeatIndefinitely, SimpleTime); return sched. scheduleJob (jobCheck, triggerCheck);} // <summary> // modify the trigger time. The job name is required, and the Modification result // CronTriggerImpl type trigger // </summary> public static void UpdateTime (string jobName, string CronTime) {TriggerKey TKey = new TriggerKey (jobName + "_ CronTrigger", jobName + "_ TriggerGroup"); CronTriggerImpl cti = sched. getTrigger (TKey) as CronTriggerImpl; cti. cronExpression = new CronExpression (CronTime); sched. rescheduleJob (TKey, cti);} // <summary> // modify the trigger time. The job name is required, and Modification result // SimpleTriggerImpl type trigger /// </summary> /// <param name = "jobName"> </param> // <param name = "SimpleTime "> minutes </param> public static void UpdateTime (string jobName, int SimpleTime) {UpdateTime (jobName, TimeSpan. fromMinutes (SimpleTime);} // <summary> // modify the trigger time. The job name is required, and Modification result // SimpleTriggerImpl type trigger // </summary> public static void UpdateTime (string jobName, TimeSpan SimpleTime) {TriggerKey TKey = new TriggerKey (jobName + "_ SimpleTrigger", jobName + "_ TriggerGroup"); SimpleTriggerImpl sti = sched. getTrigger (TKey) as SimpleTriggerImpl; sti. repeatInterval = SimpleTime; sched. rescheduleJob (TKey, sti) ;}/// <summary> /// pause all jobs /// Quartz provides many functions, you can expand it later /// </summary> public static void PauseAll () {sched. pauseAll () ;}//< summary> /// restore all jobs /// Quartz provides many functions, you can expand it later /// </summary> public static void ResumeAll () {sched. resumeAll () ;}//< summary> /// Quartz provides many functions to delete a Job, you can expand it later /// </summary> /// <param name = "JobName"> </param> public static void DeleteJob (string JobName) {JobKey jk = new JobKey (JobName, JobName + "_ Group"); sched. deleteJob (jk );} /// <summary> /// uninstall the timer /// </summary> /// <param name = "waitForJobsToComplete"> wait for the job to complete </param> public static void Shutdown (bool waitForJobsToComplete) {sched. shutdown (waitForJobsToComplete );}}}

There is only one Button on the form interface. After you click it, the current Windows form is closed, and my scheduled task is to close it.

. CloseForm (); to close the form method, call this class in another stopjob. cs class file, which needs to inherit IJob

Using Quartz; using System. collections. generic; using System. linq; using System. text; namespace winform status bar {[PersistJobDataAfterExecution] [DisallowConcurrentExecution] public class stopjob: IJob {public void Execute (IJobExecutionContext context) {Program. form. closeForm ();}}}

In the code above, the Program. form is not the Form that begins to be created, but the static form created in the Program class file. Why create another static Form? The problem of sub-threads disabling the main thread

Using System; using System. collections. generic; using System. linq; using System. windows. forms; namespace winform status bar {static class Program {public static Form1 form; // <summary> // main entry point of the application. /// </Summary> [STAThread] static void Main () {Application. enableVisualStyles (); Application. setCompatibleTextRenderingDefault (false); form = new Form1 (); Application. run (form );}}}

Reference Quartz (using Quartz;) in the Form. cs File ;)

Then, the Button event is closed.

Using Quartz; using System. text. regularExpressions; using System. windows. forms; namespace winform status bar {public partial class Form1: Form {public Form1 () {InitializeComponent ();}
Private void button_close (object sender, EventArgs e) {// cron expression reference http://www.cnblogs.com/sunjie9606/archive/2012/03/15/2397626.html QuartzManager. AddJob <stopjob> ("every 5 seconds", "*/5 ****? "); // Execute this method every five seconds} private delegate void CloseForm (); // design to multithreading. The subthread controls the control of the main thread. The InvokeRequired value is true, define the delegate so that this control behavior is the act of the main thread public void closeForm () {if (this. invokeRequired) {this. beginInvoke (new CloseForm (closeForm);} else {this. close ();}}}}

The stopjob execution time is used every five seconds. The specific explanation is introduced by another blogger.

http://www.cnblogs.com/sunjie9606/archive/2012/03/15/2397626.html

Among them, C5.dll, Common. Logging. dll, Quartz. dll file link http://pan.baidu.com/s/1hsBn1Bm (if the failure to contact the blogger)

Winform is used when I study Quartz. Net. You can try something else so that the thread problems can be avoided. This is also a problem that I did not think.

 

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.