- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.web;
- using System.Threading;
- namespace OKGO.Web
- {
- public class TestTimer
- {
- //定义变量n,Timer执行一次n自动加一,根据n来控制定时执行的时间,来解决Timer只能定时一分钟的时间范围
- static int n = 0;
- //TimerCallback 委托,GlobalTimer_ToDo表示要执行的方法
- public static Timer GlobalTimer = new Timer(new TimerCallback(GlobalTimer_ToDo), null, Timeout.Infinite, Timeout.Infinite);
- /*也可以直接定时
- GlobalTimer.Interval = 10;
- GlobalTimer.Enabled = true;
- GlobalTimerAutoReset = true;*/
- static void GlobalTimer_ToDo(object obj)
- {
- n = n + 1;
- if (n == 2)
- {
- //***这里写你要定时执行的程序
- System.Data.SqlClient.SqlDataReader dataReader = null;
- try
- {
- //is_549067
- System.IO.StreamReader sr = null;
- string str=string.Empty;
- dataReader = OKGO.DataAccess.SQLServer.ChannelProvider.Provider.GetAllRoleName();
- while (dataReader.Read() && dataReader.HasRows)
- {
- System.Net.WebRequest temp = System.Net.WebRequest.Create("http://assist57.ddt.iugame.cn/other/getUserRegDateByUid?uid=is_"+dataReader["UserName"]+"");
- //读取
- try
- {
- System.Net.WebResponse myTemp = temp.GetResponse();
- sr = new System.IO.StreamReader(myTemp.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"));
- str = sr.ReadToEnd();
- System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
- xmldoc.LoadXml(str);
- System.Xml.XmlNode nodes = xmldoc.SelectSingleNode("Result");
- string Rolename = nodes.Attributes[2].Value;//角色名称
- string RolenameTime = nodes.Attributes[3].Value;//角色创建时间
- if (Rolename == "")
- continue;
- OKGO.DataAccess.SQLServer.ChannelProvider.Provider.GetUpdateRoleName(int.Parse(dataReader["UserName"].ToString()), Rolename, RolenameTime);
- sr.Close();
- sr.Dispose();
- sr.Close();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
- catch (Exception exc)
- {
- if (dataReader != null)
- {
- dataReader.Close();
- dataReader.Dispose();
- }
- throw exc;
- }
- finally
- {
- if (dataReader != null)
- {
- dataReader.Close();
- dataReader.Dispose();
- }
- n = 0;
- }
- }
- }
- public static void Start(long a, long b)
- {
- //Timer.Change(Int32, Int32)方法用来更改计时器的启动时间和方法调用之间的间隔,用 32 位有符号整数度量时间间隔
- GlobalTimer.Change(a, b);
- }
- public static void Stop()
- {
- //Timeout.Infinite是用于指定无限长等待时间的常数
- GlobalTimer.Change(Timeout.Infinite, Timeout.Infinite);
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/250720134798.html
来源: http://www.codesnippet.cn/detail/250720134798.html