asp.net定时生成静态页面问题

liuyunluoyouyou 2009-03-26 10:55:06
是否需要在global里,做一个timer啊。
这是生成静态的方法
public void Dohtml(string Ourl, string Path)
我在global调用:

BLL.BLL hh = new BLL.BLL();
hh.Dohtml("http://localhost/default.aspx", System.Web.HttpContext.Current.Server.MapPath("./"));

但是不成功。在百度,google里搜索了无数次,仍为找到答案。请高手赐教。
-------------------------------------------------------------

我global里的代码是这样的:
protected void Application_Start(object sender, EventArgs e)
{

// 在应用程序启动时运行的代码
System.Timers.Timer myTimer = new System.Timers.Timer(1000 * 1 * 1);
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);//时间到了执行
myTimer.Interval = 1000 * 1 * 1;
myTimer.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件
myTimer.AutoReset = true;
// t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
}
private static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
{

try
{
BLL.BLL hh = new BLL.BLL();
hh.Dohtml("http://localhost/hengshuirc/default.aspx", System.Web.HttpContext.Current.Server.MapPath("./"));
}
catch { }
}
...全文
223 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
gongbenwen 2009-08-23
  • 打赏
  • 举报
回复
这个问题我也遇到过,当初自己是从网上档的代码,对多线程缺乏认知。
楼主应该知道在托管环境下如果你的前台线程执行完后,那么依赖前台线程的后台线程也会自动被abort掉,除非你在主线程中阻塞一下,等待后台线程的完成。
最好把上下文获取本地路径放到主线程中,然后作为参数传到从线程中,以下是简单的调用,这里如果参数太多或是复杂参数,你可以构造一个线程辅助类,将所有的参数放到辅助对象中,作为state的参数
protected void Application_Start(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
string mappath = context.Server.MapPath("static");
Timer time = new Timer(new TimerCallback(CreateToStaticHtml), mappath, 1000, 5000);
}
private void CreateToStaticHtml(object state)
{
FileStream fs = new FileStream(string.Format("{0}/{1}.html",state.ToString(), DateTime.Now.ToString().Replace("-","").Replace(":","").Replace(" ","")),FileMode.OpenOrCreate, FileAccess.Write,FileShare.Read);
StreamWriter sw = new StreamWriter(fs);
sw.Write("dfghjkl;dfghjkl");
sw.Close();
}
liuyunluoyouyou 2009-03-26
  • 打赏
  • 举报
回复
再顶一下,吃饭去了。
liuyunluoyouyou 2009-03-26
  • 打赏
  • 举报
回复
再顶一回,高手不来,俺就走了
liuyunluoyouyou 2009-03-26
  • 打赏
  • 举报
回复
不能沉啊。
mathieuxiao 2009-03-26
  • 打赏
  • 举报
回复
帮顶
liuyunluoyouyou 2009-03-26
  • 打赏
  • 举报
回复
顶起来,高手来
liuyunluoyouyou 2009-03-26
  • 打赏
  • 举报
回复
望高手指点。在线等!
liuyunluoyouyou 2009-03-26
  • 打赏
  • 举报
回复
自己顶

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧