Using Timer Control for a Periodic task causes many problems.
Use Master Page
using System; using System.Data; using System.Web.UI.WebControls; using System.Threading; using System.Web; namespace GlobalTimer { public partial class My_Master : System.Web.UI.MasterPage { private static Timer threadingTimer; public static string sessionId; protected void Page_Load(object sender, EventArgs e) { if (Session["UserId"] == null) { Response.Redirect("Default.aspx"); return; } if (!IsPostBack) { sessionId = Session["UserId"].ToString(); StartTimer(); } } public static void StartTimer() { if (null == threadingTimer) { threadingTimer = new Timer(new TimerCallback(CheckData), HttpContext.Current, 0, 30000); } } private static void CheckData(object sender) { ParameterCollection pc = new ParameterCollection(); string s = "Update USER_SESSIONS set TEMP_LOGOUT_DATE =@TEMP_LOGOUT_DATE where SESSION_ID=@SESSION_ID"; pc.Add("@TEMP_LOGOUT_DATE", DateTime.Now.ToString()); pc.Add("@SESSION_ID", sessionId); try { DA.SelWithParam(s, pc, CommandType.Text); } catch (Exception ex) { } } } }
No comments:
Post a Comment