还没用过定时器,问几个初级问题:
环境:web程序,vs2005,sql
想做一个定时发邮件的定时器,
请问我把已发布的程序文件传服务区上之后,如果不打开任何页面,定时器也照样执行吗?
定时器运行占服务器资源大吗?
怎样做一个定时器?
有没有参考的案例(c#)可以学习学习,如方便请发至邮箱[email protected]
非常谢谢!

解决方案 »

  1.   

    下面的代码示例启动一个计时器,该计时器在一秒(1000 毫秒)后启动,每秒增加一个刻度,直到按下“Enter”键。包含对计时器的引用的变量是类级别字段,以确保计时器在运行期间不会被垃圾回收。有关主动垃圾回收的更多信息,请参见 KeepAlive。 using System;
    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();
       } 

      

  2.   

    web页面中没办法或者说很难做定时器。
    不要用WinForm的思想来做WebForm。
      

  3.   


    你要的是服务器端的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)
    {
    }
    }
      

  4.   

    你的email程序可以放在TimerCallback 事件中
      

  5.   

    在Web中使用定时器,大多要考虑ajax应用