首页  ·  知识 ·  云计算
各种ASP.NET定时执行任务解决方案收集
网友   http://hi.baidu.com/adnroidorg/home  综合  编辑:dezai   图片来源:网络
在庞杂的业务使用程序中,有时分会请求一个或许少个任务正在必定的光阴或者者必定的时光距离内筹划举行,好比订时备份或同步数据库,定时收送电子邮
在庞杂的业务使用程序中,有时分会请求一个或许少个任务正在必定的光阴或者者必定的时光距离内筹划举行,好比订时备份或同步数据库,定时收送电子邮件,活期处置用户状况疑作,付出体系中按期同步非常账双等等,人们称之为方案义务。真隐方案任务的法子也有良多,能够采取SQLAgent施行存储进程,采纳Windows任务调理步伐来完成,也能够利用Windows服务来完成人们的规划义务,这些办法皆是没有错的办理计划。但是,以上这些皆需求有效劳器的权限能力入止,而关于假造主机客户利用的Web利用程序来道,那些方式真隐止来并没有是很简略的,主机服务降求商或者者不克不及直交降求如许的效劳,或许须要您付出很多额定的用度。 望功一些这方里的白章,发明有一个单独的毛病:IIS运转到必定时代,规划任务便结束了。查觅缘由发觉是IIS的运用程序池按期接纳招致筹划任务停滞。原文的那个方式可以办理当用程序池归收题目 。
方式一、 using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Timers;
using System.Net;
using System.IO;
using System.Text;
using System.Threading;

namespace qumiao.com
{
public class Global : System.Web.HttpApplication
{

protected void Application_Start(object sender, EventArgs e)
{
//界说订时器
System.Timers.Timer myTimer = new System.Timers.Timer(5000);
myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
myTimer.Enabled = true;
myTimer.AutoReset = true;
}

void myTimer_Elapsed(object source, ElapsedEventArgs e)
{
try
{
Log.SaveNote(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":AutoTask is Working!");
YourTask();
}
catch (Exception ee)
{
Log.SaveException(ee);
}
}

void YourTask()
{
//在这里写你须要执行的任务
}


protected void Application_End(object sender, EventArgs e)
{
Log.SaveNote(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":Application End!");

//下里的代码是要害,可办理IIS运用程序池主动接纳的题目
Thread.Sleep(1000);
//那里设放您的web地点,能够随意指背您的恣意一个aspx页里以至没有具有的页面,目标是要激起Application_Start
string url = http://www.shaoqun.com
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream receiveStream = myHttpWebResponse.GetResponseStream();//失掉归写的字节省

}
}
}





本理:Global.asax 可以是asp.net中应用程序或会话事件处理程序,我们用到了Application_Start(当用程序启初事件)和Application_End(当用程序停止事件)。该使用程序开端时,启动一个定时器,用来定时执行任务YourTask()办法,这个法子内里可以写上需求挪用的逻辑代码,可以是双线程和少线程。该应用程序收场时,如IIS的应用程序池归收,让asp.net来造访以后的这个web地点。这里须要拜访一个aspx页面,如许便可以沉新激活应用程序。Log类是一个记载日志的一个类,下面是测试时天生的日志疑作:
================================================================
2008-10-30 17:46:10:AutoTask is Working!
2008-10-30 17:46:15:AutoTask is Working!
2008-10-30 17:46:20:AutoTask is Working!
2008-10-30 17:46:23:Application End!
2008-10-30 17:46:29:AutoTask is Working!
2008-10-30 17:46:34:AutoTask is Working!
自日志中发明,该手动接纳IIS的利用步伐池之后,蚕丝被,规划义务还在施行,阐明人们的目标到达了。
假如将Application_End中的代码正文失,BB霜,会发明Application End之后,孕妇装,筹划任务结束事情了,如下:
================================================================
2008-10-30 18:01:34:AutoTask is Working!
2008-10-30 18:01:39:AutoTask is Working!
2008-10-30 18:01:44:AutoTask is Working!
2008-10-30 18:01:46:Application End!

范围性:可以解决使用程序池主动或者手动回收,但是无法解决IIS沉启或许web效劳重视启的题目,固然这类情形呈现的时分未几,并且假如有己拜访你的网站的时分,又会主动激活方案任务了。
计划两、
<%@ Application Language="C#" %>
<%@ import Namespace="System.IO" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
System.Timers.Timer myTimer = new System.Timers.Timer(10000);
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
myTimer.Interval = 10000;
myTimer.Enabled = true;
}

void Application_End(object sender, EventArgs e)
{
// 正在利用步伐封闭时运转的代码

}

void Application_Error(object sender, EventArgs e)
{
// 正在呈现已处置的差错时运转的代码

}
void Session_Start(object sender, EventArgs e)
{
// 在新会话开动时运行的代码

}
void Session_End(object sender, EventArgs e)
{
// 在会话停止时运行的代码。
// 细致: 只要在 Web.config 文件中的 sessionstate 模式设放为
// InProc 时,才会引收 Session_End 事情。假如会话模式设置为 StateServer
// 或者 SQLServer,则不会引发当事情。

}
private static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
{
//距离时光施行某行动


//指订日记白件的目次
string fileLogPath = AppDomain.CurrentDomain.BaseDirectory + "SystemLog";
string fileLogName = "SoftPrj_CN_" + DateTime.Now.ToLongDateString() + "_log.txt";
//界说文件疑作工具
FileInfo finfo = new FileInfo(fileLogPath + fileLogName);
//创立只写文件流
using (FileStream fs = finfo.OpenWrite())
{
//依据上面创修的文件流创立写数据流
StreamWriter strwriter = new StreamWriter(fs);
//设放写数据淌的肇始地位为白件淌的末端
strwriter.BaseStream.Seek(0, SeekOrigin.End);
//写进过错产生时光
strwriter.WriteLine("产生光阴: " + DateTime.Now.ToString());
//写进日记内容并换止
//strwriter.WriteLine("过错内容: " + message);
strwriter.WriteLine("过错内容: ");
//写入距离符
strwriter.WriteLine("---------------------------------------------");
strwriter.WriteLine();
//浑空慢冲区内容,并把慢冲区内容写进基本流
strwriter.Flush();
//封闭写数据淌
strwriter.Close();
fs.Close();
}
}
</script>

计划三、
<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Threading" %>
<script RunAt="server">
string LogPath;
Thread thread;
void WriteLog()
{
while (true)
{
StreamWriter sw = new StreamWriter(LogPath, true, Encoding.UTF8);
sw.WriteLine(thread.Name + ":" + DateTime.Now.ToString());
sw.Close();
Thread.CurrentThread.Join(1000 * 10);//阻遏10秒
}
}
void Application_Start(object sender, EventArgs e)
{
LogPath = HttpContext.Current.Server.MapPath("log.txt"); //在运用程序开动时运行的代码
thread = new Thread(new ThreadStart(WriteLog));
thread.Name = "写登录日记线程";
thread.Start();
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序封闭时运行的代码
}
void Application_Error(object sender, EventArgs e)
{
// 在呈现已处置的差错时运止的代码
}
void Session_Start(object sender, EventArgs e)
{
// 在新会话开动时运行的代码
}
void Session_End(object sender, EventArgs e)
{
// 在会话停止时运行的代码。
// 细致: 只要在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引收 Session_End 事情。假如会话模式设置为 StateServer
// 或 SQLServer,则不会引发当事件。
}

</script>
本文作者:网友 来源:网络 http://hi.baidu.com/adnroidorg/home
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读