效果: 通過在網(wǎng)站的Global.asax的Application_Start方法中 加入定時(shí)器 定時(shí)調(diào)用WebService 該WebService的一個(gè)方法 負(fù)責(zé)在后臺(tái) 向數(shù)據(jù)庫(kù)的某個(gè)表加入數(shù)據(jù)
步驟: 1.通過VS 新建一個(gè)網(wǎng)站 2.加入Global.asax 3.加入WebService 編輯 并 加入引用 4.對(duì)Global.asax進(jìn)行編輯 5.保存 運(yùn)行 網(wǎng)站 查看效果 ============================= 1.通過VS 新建一個(gè)網(wǎng)站
2.加入Global.asax ----------------- 其默認(rèn)內(nèi)容如下: <%@ Application Language="C#" %>
<script runat="server"> void Application_Start(object sender, EventArgs e) { // 在應(yīng)用程序啟動(dòng)時(shí)運(yùn)行的代碼 } void Application_End(object sender, EventArgs e) { // 在應(yīng)用程序關(guān)閉時(shí)運(yùn)行的代碼 } void Application_Error(object sender, EventArgs e) { // 在出現(xiàn)未處理的錯(cuò)誤時(shí)運(yùn)行的代碼 }
void Session_Start(object sender, EventArgs e) { // 在新會(huì)話啟動(dòng)時(shí)運(yùn)行的代碼 }
void Session_End(object sender, EventArgs e) { // 在會(huì)話結(jié)束時(shí)運(yùn)行的代碼。 // 注意: 只有在 Web.config 文件中的 sessionstate 模式設(shè)置為 // InProc 時(shí),才會(huì)引發(fā) Session_End 事件。如果會(huì)話模式設(shè)置為 StateServer // 或 SQLServer,則不會(huì)引發(fā)該事件。 } </script> 3.加入WebService 編輯 并 加入引用
4.對(duì)Global.asax進(jìn)行如下編輯: ---------------------------- void Application_Start(object sender, EventArgs e) { // 在應(yīng)用程序啟動(dòng)時(shí)運(yùn)行的代碼 System.Timers.Timer myTimer = new System.Timers.Timer(60000); myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent); myTimer.Interval = 60000; myTimer.Enabled = true; } private static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e) { localhost.WebService a = new localhost.WebService(); string s = a.HelloWorld(); }
5.保存 運(yùn)行 網(wǎng)站 查看效果 |