About using System.Thread.Timer in httpmodule

xiaoxiao2021-03-06  39

In use System.Thread.Timer HttpModule task can be achieved to a certain extent the timing / background task // public class ScheduleMailModule: IHttpModule {private static Timer _timer; public ScheduleMailModule () {} #region IHttpModule members

Public void init (httpapplication app) {//mapperconfig.init (); if (_timer == null) {// 5min running 5 * 60 * 1000 _Timer = New Timer (New TimalCallback (ScheduledWorkcallback), App.Context, 5 * 1000, 5 * 60 * 1000);}}

Public void dispose () {if (_timer! = null) {_timer.dispose (); _timer = null;}}

#ndregion

///

/// /// private void ScheduledWorkCallback (object state) {Email [] emails = Manage.EmailPoolManage.GetEmailsViaFlag (EmailFlag.NotSend , 2); if (emails = null) {foreach (Email email in emails) {Mail.MailMessage mailMessage = Manage.EmailPoolManage.BuildMailMessage (email);!! if (mailMessage = null) {bool bOK = Manage.EmailPoolManage.SendMail (Manage.sysmailconfigmanage.getInstance (), mailmessage); if (bok) {manage.emailpoolmanage.removeemailviaid (email.emailpool.emailid);}}}}

}} Is just a proxy method of Timer's Timercallback, which will run in a new thread in some ways to be called / or the method call, calls system.web.httpContext.current will return null *** In an ekap project for Shanghai Bell, use Ibatisnet components, namespace ibatisnet.DataMapper.SessionContainer {///

/// build a session container for a windows or web context. /// public class SessionContainerFactory {#region Methods /// /// Get a session container for a Windows or Web context. /// /// static public ISessionContainer GetSessionContainer () {If (System.Web.httpContext.current == null) {Return New WindowSessionContainer ();} else {return new websessionContainer ();}} #ENDREGON}}} #ENDREGON}}}} #ENDREGON}}}} #ENDREGON}}}} #endregion}} / Session container for web application. /// public class WebsessionContainer: iSessionContainer {#Region Constants

///

/// token for sqlmapconfig xml root. /// private const string local_session = "_ibatis_local_sqlmapsession_";

#ndregion

#Region Constructionor (s) / Destructor

///

/// Constructor /// public websessionContainer () {system.Web.httpContext.current.Items [local_session] = null;} #ENDREGION

#Region ISessionContainer Members

#Region Properties ///

/// Get The local session /// public sqlmapsession localsession {get {// add by Sharper if (System.Web.httpContext.current! = null) {Return SYSTEM .Web.httpContext.current.items [local_session] as sqlmapsession;

} else {return null;}

} #ENDREGION

#region methods ///

/// store the local session on the contact is unique for each thread. /// /// The session to store public void store (sqlmapsession session) {system.Web.httpContext.current.Items [local_session] = session;} /// /// remove the local session from the Container. /// public void dispose () {system.Web.httpContext.current.items [local_session] = null;}

#ndregion

#ndregion} where the original localsession property does not judge whether system.Web.httpContext.current is NULL so that a nullreferenceException is triggered.

转载请注明原文地址:https://www.9cbs.com/read-73396.html

New Post(0)