資源簡介
調度實現定時任務,定時實現某些業務,可以自由選擇調度重復次數。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Threading.Tasks;
using?System.Timers;
using?Topshelf;
namespace?TimerAndDispatch
{
????class?Program
????{
????????#region?Timer定時任務
????????static?void?Main1(string[]?args)
????????{
????????????System.Timers.Timer?timer?=?new?System.Timers.Timer();
????????????timer.Enabled?=?true;
????????????timer.Interval?=?3000;?//執行間隔時間單位為毫秒;?這里實際間隔為3秒?
????????????timer.Start();
????????????timer.Elapsed?+=?new?System.Timers.ElapsedEventHandler(test1);
????????????Console.ReadKey();
????????}
????????private?static?void?test1(object?source?ElapsedEventArgs?e)
????????{
????????????Console.WriteLine(“OK?test?event?is?fired?at:?“?+?DateTime.Now.ToString());
????????}
????????static?void?Main2(string[]?args)
????????{
????????????System.Timers.Timer?timer?=?new?System.Timers.Timer();
????????????timer.Enabled?=?true;
????????????timer.Interval?=?1000;//執行間隔時間單位為毫秒;此時時間間隔為1分鐘??
????????????timer.Start();
????????????timer.Elapsed?+=?new?System.Timers.ElapsedEventHandler(test2);
????????????Console.ReadKey();
????????}
????????private?static?void?test2(object?source?ElapsedEventArgs?e)
????????{
????????????if?(DateTime.Now.Second?==?30)??//如果當前時間是30秒
????????????????Console.WriteLine(“OK?event?fired?at:?“?+?DateTime.Now.ToString());
????????}
????????#endregion
????????#region?任務調度
????????static?void?Main(string[]?args)
????????{
????????????//NLog.ILogger?logger?=?NLog.LogManager.GetCurrentClassLogger();
????????????//logger.Error(“111111“);
????????????//?配置和運行宿主服務
????????????HostFactory.Run(x?=>
????????????{
????????????????//?指定服務類型。這里設置為?TestSchedule
????????????????x.Service(s?=>
????????????????{
????????????????????//?通過?new?TestSchedule()?構建一個服務實例?
????????????????????s.ConstructUsing(name?=>?new?TestSchedule());
????????????????????//?當服務啟動后執行什么
????????????????????s.WhenStarted(tc?=>?tc.Start());
????????????????????//?當服務停止后執行什么
????????????????????s.WhenStopped(tc?=>?tc.Stop());
????????????????});
????????????????//?服務用本地系統賬號來運行
????????????????x.RunAsLocalSystem();
????????????????//?服務描述信息
????????????????x.SetDescription(“TestSchedule“);
????????????????//?服務顯示名稱
????????????????x.SetDisplayName(“TestScheduleService“);
????????????????//?服務名稱
????????????????x.SetServiceName(“TestScheduleService“);
????????????});
????????}
????????#endregion
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????..A..H.?????74240??2019-01-15?10:30??TimerAndDispatch\.vs\TimerAndDispatch\v15\.suo
?????文件?????778240??2019-01-15?10:18??TimerAndDispatch\.vs\TimerAndDispatch\v15\sqlite3\storage.ide
?????文件???????9490??2018-11-06?09:32??TimerAndDispatch\packages\NLog.4.5.11\.signature.p7s
?????文件?????621056??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\monoandroid44\NLog.dll
?????文件????1355032??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\monoandroid44\NLog.xm
?????文件?????688128??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\net35\NLog.dll
?????文件????1490624??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\net35\NLog.xm
?????文件?????695296??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\net40-client\NLog.dll
?????文件????1495274??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\net40-client\NLog.xm
?????文件?????704000??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\net45\NLog.dll
?????文件????1510833??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\net45\NLog.xm
?????文件?????603136??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\netstandard1.3\NLog.dll
?????文件????1317777??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\netstandard1.3\NLog.xm
?????文件?????635392??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\netstandard1.5\NLog.dll
?????文件????1361180??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\netstandard1.5\NLog.xm
?????文件?????654848??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\netstandard2.0\NLog.dll
?????文件????1401653??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\netstandard2.0\NLog.xm
?????文件?????556032??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\sl4\NLog.dll
?????文件????1085995??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\sl4\NLog.xm
?????文件?????560640??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\sl5\NLog.dll
?????文件????1090250??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\sl5\NLog.xm
?????文件?????543744??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\wp8\NLog.dll
?????文件????1038453??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\wp8\NLog.xm
?????文件?????619008??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\xamarinios10\NLog.dll
?????文件????1350647??2018-11-06?17:07??TimerAndDispatch\packages\NLog.4.5.11\lib\xamarinios10\NLog.xm
?????文件????3791407??2019-01-15?09:45??TimerAndDispatch\packages\NLog.4.5.11\NLog.4.5.11.nupkg
?????文件???????9461??2018-10-07?05:31??TimerAndDispatch\packages\Quartz.3.0.7\.signature.p7s
?????文件?????866816??2018-10-07?12:27??TimerAndDispatch\packages\Quartz.3.0.7\lib\net452\Quartz.dll
?????文件?????225172??2018-10-07?12:27??TimerAndDispatch\packages\Quartz.3.0.7\lib\net452\Quartz.pdb
?????文件????1040642??2018-10-07?12:27??TimerAndDispatch\packages\Quartz.3.0.7\lib\net452\Quartz.xm
............此處省略81個文件信息
評論
共有 條評論