读书人

如何写定时器

发布时间: 2013-11-19 22:55:29 作者: rapoo

怎么写定时器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Threading;


public partial class Test : System.Web.UI.Page
{
static int a = 0;
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{


Test b = new Test();

AutoResetEvent myResetEvent = new AutoResetEvent(false);

TimerCallback timerDelegate = new TimerCallback(b.test);
Timer c = new Timer(timerDelegate, null, 0, 1);
}
public void test(object b)
{

a++;
System.Web.HttpContext.Current.Response.Write(a);
return;
}


每次到System.Web.HttpContext.Current.Response.Write(a);这都报错“未将对象引用甚至到对象的实例”
不知道为什么,求解 定时器
[解决办法]
不建议在asp.net的服务器端使用定时器,建议用客户端js的setInterval代替。
[解决办法]
在asp.net的服务器端使用定时器太浪费资源了 增加了系统的整体耗损 卡死也很正常,建议用客户端js的setInterval代替。 百度 谷歌 js Timer 一看就知道了
[解决办法]
你这个肯定不行啊,web是无状态的
[解决办法]
用个Timer控件吧:
前台:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="3000" ontick="Timer1_Tick">
</asp:Timer>

<br />
<asp:Label ID="lbTime" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>

后台:
private int index
{
get
{
object o = ViewState["index "];
return (o == null) ? 0 : (int)o;//设定起始时间
}
set
{
ViewState["index "] = value;
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
this.index++;
if (this.index == 0) //设定时间到了
{
this.Timer1.Enabled = false;//设置Timer控件不可见
//时间到时,此处可编写方法
}
else
{
//显示时间
this.lbTime.Text = this.index / 60 + "分" + this.index % 60 + "秒";
}
}
[解决办法]
http是基于请求的,定时器开启后请求早已返回,再运行就没有Request,更谈不上Response
------解决方案--------------------


可以明确地告诉你,你这个完全不行。
你的要求就是服务器端推送消息去客户端
可以尝试
1 html5的websocket
2 singalR
3 AJAX轮询
4 flash
[解决办法]

引用:
Quote: 引用:

可以明确地告诉你,你这个完全不行。
你的要求就是服务器端推送消息去客户端
可以尝试
1 html5的websocket
2 singalR
3 AJAX轮询
4 flash

我这个是想写在global里定时同步多个服务器的,在页面的基类里先做个实验,结果实验失败了,你给点建议呗,这个实验怎么才能成功,要是换成到global里是不是就行啦


可行,但仍不稳定,web服务器拿IIS来说,进程池默认会空闲20分钟(没有收到请求)后自动回收,这样即使放在global中也会停止,除非你禁用该选项
还有一些其他原因也会导致程序挂掉,定时任务最好是放到windows服务中,随机器自启动
[解决办法]
 
void Application_Start(object sender, EventArgs e)
{
//在应用程序启动时运行的代码
SetTimer();
this.Dispose();
}
public static void SetTimer()
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimer);
aTimer.Enabled = true;
aTimer.Interval = 1000;
}

public static void OnTimer(Object source, System.Timers.ElapsedEventArgs e)
{
int days = e.SignalTime.Day;
int hours = e.SignalTime.Hour;
int Minutes = e.SignalTime.Minute;
int Seconds = e.SignalTime.Second;

int iHour = 23;
int iMinute = 59;
int iSecond = 59;
if (hours == iHour && Minutes == iMinute && Seconds == iSecond)
{
EIS.Cgiac.Cgiac.BLL.MemberInfo bll = new EIS.Cgiac.Cgiac.BLL.MemberInfo();
bll.UpdateQueryNo();
}
int iDays = 1;
int iHours = 00;
int iMinutes = 00;
int iSeconds = 00;

if (days == iDays && hours == iHours && Minutes == iMinutes && Seconds == iSeconds)
{
DateTime date = DateTime.Now;
date = date.AddDays(1 - date.Day).AddMonths(-1);
EIS.Cgiac.Cgiac.BLL.SystemLog bll = new EIS.Cgiac.Cgiac.BLL.SystemLog();
bll.Delete(date);
}
}


我把我以前做的定时器代码贴出来给你 这个代码是写在Global.asax中的 也许对你有帮助;
我做的功能是
每天23:59:59 更新一次
每个月1号的0点 删除一次
[解决办法]
引用:
引用
可行,但仍不稳定,web服务器拿IIS来说,进程池默认会空闲20分钟(没有收到请求)后自动回收,这样即使放在global中也会停止,除非你禁用该选项
还有一些其他原因也会导致程序挂掉,定时任务最好是放到windows服务中,随机器自启动

为什么在页面的基类里测试就会有http请求返回的问题,如果我想在基类里测试我应该怎么写?

页面是基于Page:IHttpHandler的,这个生命周期很短,一般几秒(打开见面的过程)就完了
Global则是基于HttpApplication的,而它的宿主就是工作进程,只要进程仍正常活动着,它就不会消亡

读书人网 >asp.net

热点推荐