日期:2014-05-18  浏览次数:20511 次

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

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

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();
   } 
}

------解决方案--------------------

你要的是服务器端的Timer. 只要第一个访问者的第一次请求 (Application_Start 事件)即可启动Timer. 

C# code
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();
    }
}

------解决方案--------------------
在Web中使用定时器,大多要考虑ajax应用