在新的线程里不能操作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