首页 新闻 搜索 专区 学院

当iBATIS遇到Timer

0
悬赏园豆:20 [已解决问题] 解决于 2007-11-13 09:09
为了在程序中实现定时任务,我在Global.asax里面加了一个System.Threading.Timer类的实例,然后在 Application_Start()中将其初始化,开始还没问题,前两天遇到了一个奇怪的异常: WebSessionStore: Could not obtain reference to HttpContext.<br> <br> 在网上也没找到解决方案,最近也没时间去仔细看源码,搞不懂在Web项目中如何结合使用iBATIS和Timer.
问题补充: 看了 没剑 的回答,得到一点提示,可以通过静态变量来保存HttpContext,在使用一个Timer时是没问题的,但使用两个Timer时却有问题了,不知为何。
Anders Cui的主页 Anders Cui | 小虾三级 | 园豆:1570
提问于:2007-11-12 11:21
< >
分享
最佳答案
0
在新的线程里不能操作IIS线程...我以前也碰到过,我是用多线程来更新网站的在线人数,你可以参考一下我的代码: Imports System.Data.SqlClient Imports System.Threading Imports System.Runtime.InteropServices Imports System.Web Public Class AutoUpdate Protected appHandle As GCHandle Public Sub New() 'If System.Web.HttpContext.Current.Application("UpdateTime") Is Nothing Then '初始化 appHandle = GCHandle.Alloc(System.Web.HttpContext.Current, GCHandleType.Normal) Dim th As New Thread(New ThreadStart(AddressOf startU)) th.IsBackground = True th.Start() End Sub #Region "更新操作" Public Sub startU() Dim context As HttpContext = CType(appHandle.Target, System.Web.HttpContext) Dim App As HttpApplicationState = context.Application Dim Match As New Match Dim Common As New Common While True App.Lock() Try Dim OnlineUserList As List(Of OnlineUser) = CType(App("OnLineUser"), List(Of OnlineUser)) If OnlineUserList IsNot Nothing Then If OnlineUserList.Count > 0 Then Dim Temp() As OnlineUser = OnlineUserList.ToArray Dim OnlineRobotList As Hashtable = Nothing Try OnlineRobotList = CType(App("OnLineRobot"), Hashtable) '已在线的机器人列表 Catch ex As Exception OnlineRobotList = New Hashtable(100) End Try For Each user As OnlineUser In Temp If user IsNot Nothing Then 'If Match.IsRobot(user.UserName) = False Then If DateDiff(DateInterval.Minute, user.OnlineTime, Now) > 30 Then '10分钟就算超时 Dim Finder As New Finder(user.UserName) Dim c A
没剑 | 菜鸟二级 |园豆:430 | 2007-11-12 12:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册