C# 控制asp.net网站自动更新数据代码
发布时间:2020-12-15 17:55:35 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 添加更新数据在类中写好,然后在Global.asax里面调用这个类,设置 TestTimer.Start(0,30000);为1分钟 using System;using System.Collections.Generic
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考
添加更新数据在类中写好,然后在Global.asax里面调用这个类,设置 TestTimer.Start(0,30000);为1分钟
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); } } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |