global.asax 文件之我见

我是刚刚出道的菜鸟,由于网站上面涉及到新闻显示,公司要求写一个 自动采集的功能 也就是在每间隔多少时间采集一次新闻信息 使得网站新闻更新 本来说是在window 下设置任务计划程序 可以设置在什么时候做什么操作 可是我不知道怎样将 C#中的.cs 文件如何转成 windows 任务计划程序可执行的文件 所以采用了 多线程的方法 在global文件中用timer 计时器来监视每隔多久程序被执行一次 这有代码 我写的事例 大家参考参考啊 希望对大家有用

<%@ Application Language="C#" %>

<script runat="server">

void Application_Start(object sender, EventArgs e)
{
//在应用程序启动时运行的代码
//Session["state"] = "off";//尝试有误 改用Application 对象则可以 global 文件是用在全局范围内的 session 则是一个客户一个怎么能在global里设置

//定义一个进程
System.Threading.Thread myThread = new System.Threading.Thread(new System.Threading.ThreadStart(myStart));
//进程开始
myThread.Start();
}
//进程执行程序
void myStart()
{
//1000ms=1s
System.Timers.Timer time =new System.Timers.Timer(1000*60*2);
time.Enabled=true;
time.Elapsed += new System.Timers.ElapsedEventHandler(time_Elapsed);
time.AutoReset = true;
time.Start();
}
//timer 触发事件
void time_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
System.DateTime dt = System.DateTime.Now;
string path = AppDomain.CurrentDomain.BaseDirectory + "/Log/" + "Log_" + dt.ToString("yymmdd") + ".txt";
if (!System.IO.Directory.Exists(path))
{
//System.IO.Directory.CreateDirectory(path);
System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
sw.WriteLine(DateTime.Now + "ceshi!");
sw.Flush();
sw.Close();
fs.Close();
}
else
{
System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
sw.WriteLine(DateTime.Now + "ceshi!");
sw.Flush();
sw.Close();
fs.Close();
}
}



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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值