还没用过定时器,问几个初级问题:
环境:web程序,vs2005,sql
想做一个定时发邮件的定时器,
请问我把已发布的程序文件传服务区上之后,如果不打开任何页面,定时器也照样执行吗?
定时器运行占服务器资源大吗?
怎样做一个定时器?
有没有参考的案例(c#)可以学习学习,如方便请发至邮箱[email protected]
非常谢谢!
环境:web程序,vs2005,sql
想做一个定时发邮件的定时器,
请问我把已发布的程序文件传服务区上之后,如果不打开任何页面,定时器也照样执行吗?
定时器运行占服务器资源大吗?
怎样做一个定时器?
有没有参考的案例(c#)可以学习学习,如方便请发至邮箱[email protected]
非常谢谢!
using System.Threading;public class Example
{
private static Timer ticker;
public static void TimerMethod(Object state)
{
Console.Write(".");
}
public static void Main()
{
ticker = new Timer(TimerMethod, null, 1000, 1000); Console.WriteLine("Press the Enter key to end the program.");
Console.ReadLine();
}
}
不要用WinForm的思想来做WebForm。
你要的是服务器端的Timer. 只要第一个访问者的第一次请求 (Application_Start 事件)即可启动Timer. public class Global_asax : System.Web.HttpApplication
{
private static TokenUpdateAgent _token_update_agent = new TokenUpdateAgent();
void Application_Start(object sender, EventArgs e)
{
int module_id = App.Shared.Constants.GetPluginID;
MIA.MVP.HQ.Framework.Security.HQUserGroupModuleSecurityCache.GetModuleSecuirty(module_id);
_token_update_agent.Initialize();
}
}
Timer 程序可以是一个Class在Assembly中public sealed class TokenUpdateAgent
{
private System.Threading.Timer _timer;
private int _interval;
public int Interval {
get { return _interval; }
set { _interval = value; }
}
public TokenUpdateAgent()
{
}
public void Initialize()
{
Interval = 1000;
_timer = new System.Threading.Timer(TimerCallback, null, System.Threading.Timeout.Infinite, Interval * 1000);
}
private void StartAgent()
{
_timer.Change(0, Interval * 1000);
}
private void StopAgent()
{
_timer.Change(System.Threading.Timeout.Infinite, Interval * 1000);
}
private static void TimerCallback(object state)
{
}
}