(i) Create a service Quarzservice
Using System.ServiceProcess;
Using System.Text;
Using Quartz;
Using Quartz.impl;
Using WinNet.Log;
Namespace Quarzservice
{
public partial class Quartzservice:servicebase
{
Private IScheduler Scheduler;
Public Quartzservice ()
{
InitializeComponent ();
Ischedulerfactory schedulerfactory = new Stdschedulerfactory ();
Scheduler = Schedulerfactory.getscheduler ();
}
protected override void OnStart (string[] args)
{
Scheduler. Start ();
Writelog.logtxt ("Quartz service successfully started", "Scheduler");
}
protected override void OnStop ()
{
Scheduler. Shutdown ();
Writelog.logtxt ("Quartz service terminated successfully", "Scheduler");
}
protected override void OnPause ()
{
Scheduler. Pauseall ();
}
protected override void OnContinue ()
{
Scheduler. Resumeall ();
}
}
}
(ii) QUARTZ.CONFIG configuration
# can configure your scheduler in either <quartz> configuration sections
# or in Quartz properties file
# Configuration section has precedence
Quartz.scheduler.instanceName = Serverscheduler
# Configure thread Pool info
Quartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, quartz
Quartz.threadPool.threadCount = 10
Quartz.threadPool.threadPriority = Normal
# Job Initialization plugin handles our XML reading, without it defaults is used
Quartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, quartz
Quartz.plugin.xml.fileNames = ~/quartz_jobs.xml
# Export this server to remoting context
Quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, quartz
Quartz.scheduler.exporter.port = 555
Quartz.scheduler.exporter.bindName = Quartzscheduler
Quartz.scheduler.exporter.channelType = TCP
Quartz.scheduler.exporter.channelName = Httpquartz
Write Log method:
Using System.Text;
Using System.Windows.Forms;
Using System.IO;
Namespace WinNet.Log
{
public class Writelog
{
Public Writelog ()
{
}
public static void Logtxt (String Msg, string name)
{
String logPath = Path.getdirectoryname (Application.executablepath);
System.IO.StreamWriter SW = System.IO.File.AppendText (LogPath + "/" + name + "_" + DateTime.Now.ToString ("yyyy-mm-dd") + " . txt ");
Sw. WriteLine (DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss") + MSG);
Sw. Close ();
Sw. Dispose ();
}
}
}
(iii) Creation of a task: Winnet.jobtask
Using Quartz;
Using WinNet.Log;
Namespace Winnet.jobtask
{
public class Jobgeneral:ijob
{
private static readonly Common.Logging.ILog logger = Common.Logging.LogManager.GetLogger ( System.Reflection.MethodBase.GetCurrentMethod (). DeclaringType);
#region IJob Members
public void Execute (Ijobexecutioncontext context)
{
Try
{
Writelog.logtxt ("DemoJob1 task starts running", "Scheduler");
for (int i = 0; i <; i++)
{
Writelog.logtxt ("DemoJob1 is running" + I, "scheduler");
}
Writelog.logtxt ("DemoJob1 Task Run End", "Scheduler");
}
catch (Exception ex)
{
Writelog.logtxt ("Error:" +ex. ToString (), "Scheduler");
}
}
#endregion
}
}
(iv ) installation and uninstallation of services
Installation Services, written as bat files
%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe QuarzService.exe
Net Start Quarzservice
sc config quarzservice start= auto
Uninstalling the service, written as a bat file
%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe/u QuarzService.exe
Source code: HTTP//files. Cnblogs. Com/files/aran/serviceschedulerdesign.rar
The Windowservice of Quartz