在前台用javascrip实现定时执行任务,
<%@ 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)
{ // 在会话结束时运行的代码 }
</script> |