定时异步调用的案例

            //创建代理对象TimerCallback,该代理将被定时调用
            TimerCallback GetUserListDelegate = new TimerCallback(GetUserList);
            //创建一个时间间隔为*s的定时器
             GetUserListTimer = new System.Threading.Timer(GetUserListDelegate, null, 10, 5000);//每5秒执行一次

            public void GetUserList(Object str){}//注意:1.GetUserListTimer 的定义必须在全局变量里,否则c#会当做垃圾回收

                                                                           2.被调用的方法必须传一个Object类型

 

 

------------------------第二种---------------------------------------------------

  System.Timers.Timer t = new System.Timers.Timer(10000);//实例化Timer类,设置间隔时间为10000毫秒;    
            t.Elapsed += new System.Timers.ElapsedEventHandler(TimeElapse);//到达时间的时候执行事件;    
            t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);    
            t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;  

 

public void TimeElapse(object source, System.Timers.ElapsedEventArgs e)
        {

 

}

posted on 2013-08-28 11:09  咖啡加点盐  阅读(217)  评论(0编辑  收藏  举报