Quartz.NET是一個開源的作業調度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#寫成,可用于winform和asp.net應用中。它提供了巨大的靈活性而不犧牲簡單性。你能夠用它來為執行一個作業而創建簡單的或復雜的調度。它有很多特征,如:數據庫支持,集群,插件,支持cron-like表達式等等
以上介紹是從博客園張善友(http://m.survivalescaperooms.com/shanyou/archive/2007/08/25/quartznettutorial.html)的博客摘錄,可登錄他博客具體了解quartz.net。
我在這里只講具體在項目中的實現:
通過Windows服務調用Quartz.net,然后Quartz.net 調用WinForm消息窗口,實現計劃任務消息推送。
【Windows服務】-->【Quartz.net】 --> 【Winform .exe】 --> 【程序打包】
項目解決方案,如圖所示:

1、打開vs2010--新建項目--選擇"Windows服務",我這里命名為"QuartzService". 創建好后首先映入我們眼簾的是QuartzService.cs[設計]視圖,右鍵點設計視圖選擇"添加安裝程序",如下圖:

注釋:(創建一個Windows服務,僅用InstallUtil程序去安裝這個服務是不夠的。你必須還要把一個服務安裝程序添加到你的Windows服務當中,這樣便于InstallUtil或是任何別的安裝程序知道應用你服務的是怎樣的配置設置)
2、切換到剛被添加的PRojectInstaller的設計視圖,設置serviceInstaller1組件的屬性: StartType = Automatic;ServiceName =QuartzService;
設置serviceProcessInstaller1組件的屬性Account = LocalSystem;如下圖:

3、QuartzService中添加Quartz.dll ,log4net.dll引用,在QuartzService.cs文件中引用命名空間 Quartz;Quartz.Impl; log4net;

右鍵點擊QuartzService項目,屬性-目標框架 ,選擇.net Framwork 4,如下圖:

4、添加app.config配置文件,具體配置如下:

1 <?xml version="1.0"?> 2 <configuration> 3 <configSections> 4 <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/> 5 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> 6 <sectionGroup name="common"> 7 <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/> 8 </sectionGroup> 9 </configSections>10 <common>11 <logging>12 <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net">13 <arg key="configType" value="INLINE"/>14 </factoryAdapter>15 </logging>16 </common>17 <log4net>18 <appender name="InfoFileAppender" type="log4net.Appender.RollingFileAppender">19 <file value="log/" />20 <appendToFile value="true" />21 <param name="DatePattern" value="yyyyMMdd".txt"" />22 <rollingStyle value="Date" />23 <maxSizeRollBackups value="100" />24 <maximumFileSize value="1024KB" />25 <staticLogFileName value="false" />26 <Encoding value="UTF-8" />27 <filter type="log4net.Filter.LevelRangeFilter">28 <param name="LevelMin" value="INFO" />29 <param name="LevelMax" value="INFO" />30 </filter>31 <layout type="log4net.Layout.PatternLayout">32 <conversionPattern value="%date %-5level %logger - %message%newline" />33 </layout>34 </appender>35 <appender name="ErrorFileAppender" type="log4net.Appender.RollingFileAppender">36 <file value="log/error.txt" />37 <appendToFile value="true" />38 <rollingStyle value="Size" />39 <maxSizeRollBackups value="100" />40 <maximumFileSize value="10240KB" />41 <staticLogFileName value="true" />42 <Encoding value="UTF-8" />43 <filter type="log4net.Filter.LevelRangeFilter">44 <param name="LevelMin" value="WARN" />45 <param name="LevelMax" value="FATAL" />46 </filter>47 <layout type="log4net.Layout.PatternLayout">48 <conversionPattern value="%date %-5level %logger - %message%newline" />49 </layout>50 </appender>51 <root>52 <level value="INFO" />53 <appender-ref ref="InfoFileAppender" />54 <appender-ref ref="ErrorFileAppender" />55 </root>56 </log4net>57 58 <!-- 59 We use quartz.config for this server, you can always use configuration section if you want to.60 Configuration section has precedence here. 61 -->62 <appSettings>63 <!-- YYC.WebService URL地址 -->64 <add key="URL" value="http://localhost:43093/WebService.asmx" />65 </appSettings>66 <!--67 <quartz >68 </quartz>69 -->70 <startup>71 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>72 </startup>73 </configuration>View Code
1、在解決方案中添加新項-選擇"類庫",我這里命名為Quartz.Net.JobLibrary,Quartz.Net.JobLibrary用來實現多個"作業"類。Quartz.Net.JobLibrary類庫中添加Quartz.dll ,log4net.dll引用。
Quartz.Net.JobLibrary類庫中添加一個類Interop.cs,這個類是為了解決在Win7中出現【交互式檢測】彈窗,博客園李敬然(http://m.survivalescaperooms.com/gnielee/archive/2010/04/07/session0-isolation-part1.html)的博客詳細談到 穿透Session 0 隔離,具體代碼如下:

1 using System; 2 using System.Runtime.InteropServices; 3 4 namespace Quartz.Net.JobLibrary 5 { 6 public class Interop 7 { 8 public static IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero; 9 public static void ShowMessageBox(string message, string title) 10 { 11 int resp = 0; 12 WTSSendMessage( 13 WTS_CURRENT_SERVER_HANDLE, 14 WTSGetActiveConsoleSessionId(), 15 title, title.Length, 16 message, message.Length, 17 0, 0, out resp, false); 18 } 19 [DllImport("kernel32.dll", SetLastError = true)] 20 public static extern int WTSGetActiveConsoleSessionId(); 21 [DllImport("wtsapi32.dll", SetLastError = true)] 22 public static extern bool WTSSendMessage( 23 IntPtr hServer, 24 int SessionId, 25 String pTitle, 26 int TitleLength, 27 String pMessage, 28 int MessageLength, 29 int Style, 30 int Timeout, 31 out int pResponse, 32 bool bWait); 33 34 public static void CreateProcess(string app, string path) 35 { 36 IntPtr hToken; 37 IntPtr hDupedToken = IntPtr.Zero; 38 39 PROCESS_INFORMATION pi = new PROCESS_INFORMATION(); 40 SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES(); 41 sa.Length = Marshal.SizeOf(sa); 42 43 STARTUPINFO si = new STARTUPINFO(); 44 si.cb = Marshal.SizeOf(si); 45 46 int dwSessionID = WTSGetActiveConsoleSessionId(); 47 bool result = WTSQueryUserToken(dwSessionID, out hToken); 48 49 if (!result) 50 { 51 ShowMessageBox("WTSQueryUserToken failed", "AlertService Message"); 52 } 53 54 result = DuplicateTokenEx( 55 hToken, 56 GENERIC_ALL_access, 57 ref sa, 58 (int)SECURITY
新聞熱點
疑難解答